Compare commits
19 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
68427c4a11
|
|||
|
8dd9e85b77
|
|||
|
0c7d2ad518
|
|||
|
0289dd4684
|
|||
|
e2d43315e3
|
|||
|
0e1c2c6350
|
|||
|
da85fc94b1
|
|||
|
c66d782eab
|
|||
|
358b44ab81
|
|||
|
5957d67bc3
|
|||
|
04410ea9e7
|
|||
|
a514cdc69f
|
|||
|
e822ba1f54
|
|||
|
3c925d8268
|
|||
|
c9073ff4a7
|
|||
|
88324c21c2
|
|||
|
ffc9741d1e
|
|||
| 5638315031 | |||
| 2e43a5e396 |
61
CHANGELOG.md
61
CHANGELOG.md
@@ -1,4 +1,46 @@
|
||||
## v1.0.1 更新日志
|
||||
# Changelog
|
||||
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
暂无。
|
||||
|
||||
## [0.1.11] - 2026-03-23
|
||||
|
||||
### ✨ 新功能
|
||||
- 新增配置导出导入功能,支持加密保护
|
||||
- Profile 支持 Token 管理(PAT 等)
|
||||
- 自动生成和维护 Keep a Changelog 格式的变更日志
|
||||
- 交互式命令行界面,支持预览和确认
|
||||
|
||||
### 🔐 安全特性
|
||||
- 敏感数据加密存储(API 密钥等)
|
||||
- 使用系统密钥环安全保存凭证
|
||||
|
||||
### 🔧 其他变更
|
||||
- 优化 diff 截断逻辑,使用字符边界确保多字节字符安全
|
||||
- 改进配置管理器,支持修改追踪
|
||||
|
||||
## [0.1.9] - 2026-03-06
|
||||
|
||||
### 🐞 错误修复
|
||||
- 修复diff截断时的字符边界问题
|
||||
|
||||
## [0.1.7] - 2026-02-14
|
||||
|
||||
### 🐞 错误修复
|
||||
- 修复 `changelog` 命令默认覆盖文件的问题,现改为智能追加新版本条目到头部之后
|
||||
|
||||
### 🔧 其他变更
|
||||
- 清理 `formatter.rs` 中未使用的函数(`format_commit_date`、`format_changelog_date`、`format_tag_name`、`truncate`、`format_markdown_list`、`format_changelog_section`、`format_git_config_key`)
|
||||
- 清理 `validators.rs` 中未使用的函数(`validate_ssh_key`)
|
||||
- 移除 `changelog` 命令的 `--prepend` 参数(默认行为已改为追加)
|
||||
|
||||
## [0.1.4] - 2026-02-01
|
||||
|
||||
### ✨ 新功能
|
||||
- 新增 `test3.txt`,支持中文输出测试
|
||||
@@ -20,3 +62,20 @@
|
||||
### 🔧 其他变更
|
||||
- 新增个人访问令牌、使用统计与配置校验功能
|
||||
- 添加 `test2.txt` 占位文件
|
||||
|
||||
## [0.1.0] - 2026-01-30
|
||||
|
||||
### Added
|
||||
- Initial project structure
|
||||
- Core functionality for git operations
|
||||
- LLM integration
|
||||
- Configuration management
|
||||
- CLI interface
|
||||
|
||||
### Features
|
||||
- **Commit Generation**: Automatically generate conventional commit messages from git diffs
|
||||
- **Profile Management**: Switch between multiple Git identities for different contexts
|
||||
- **Tag Management**: Create annotated tags with AI-generated release notes
|
||||
- **Changelog**: Generate and maintain changelog in Keep a Changelog format
|
||||
- **Security**: Encrypt SSH passphrases and API keys
|
||||
- **Interactive UI**: Beautiful CLI with prompts and previews
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "quicommit"
|
||||
version = "0.1.4"
|
||||
version = "0.1.11"
|
||||
edition = "2024"
|
||||
authors = ["Sidney Zhang <zly@lyzhang.me>"]
|
||||
description = "A powerful Git assistant tool with AI-powered commit/tag/changelog generation(alpha version)"
|
||||
@@ -66,6 +66,9 @@ argon2 = "0.5"
|
||||
rand = "0.8"
|
||||
base64 = "0.22"
|
||||
|
||||
# System keyring for secure API key storage
|
||||
keyring = { version = "3.6.3", features = ["apple-native", "windows-native", "sync-secret-service"] }
|
||||
|
||||
# Interactive editor
|
||||
edit = "0.1"
|
||||
|
||||
|
||||
60
README.md
60
README.md
@@ -6,8 +6,12 @@ A powerful AI-powered Git assistant for generating conventional commits, tags, a
|
||||
|
||||
[Still in early development, some features may not be complete. Feedback and contributions are welcome.]
|
||||
|
||||
> ⚠️ **Important Notice**: QuiCommit now uses system keyring to store API keys securely. This change may cause breaking changes to your existing configuration. If you encounter issues after updating, please run `quicommit config reset --force` to reset your configuration, then reconfigure your settings.
|
||||
|
||||

|
||||

|
||||

|
||||
|
||||
|
||||
## Features
|
||||
|
||||
@@ -16,8 +20,10 @@ A powerful AI-powered Git assistant for generating conventional commits, tags, a
|
||||
- **Profile Management**: Manage multiple Git identities with SSH keys and GPG signing support
|
||||
- **Smart Tagging**: Semantic version bumping with AI-generated release notes
|
||||
- **Changelog Generation**: Automatic changelog generation in Keep a Changelog format
|
||||
- **Security**: Encrypt sensitive data
|
||||
- **Security**: Use system keyring to store API keys securely
|
||||
- **Interactive UI**: Beautiful CLI with previews and confirmations
|
||||
- **Multi-language Support**: Output in 7 languages (English, Chinese, Japanese, Korean, Spanish, French, German)
|
||||
- **Config Export/Import**: Backup and restore configuration with optional encryption
|
||||
|
||||
## Installation
|
||||
|
||||
@@ -159,30 +165,30 @@ quicommit profile token
|
||||
```bash
|
||||
# Configure Ollama (local)
|
||||
quicommit config set-llm ollama
|
||||
quicommit config set-ollama --url http://localhost:11434 --model llama2
|
||||
quicommit config set-llm ollama --url http://localhost:11434 --model llama2
|
||||
|
||||
# Configure OpenAI
|
||||
quicommit config set-llm openai
|
||||
quicommit config set-openai-key YOUR_API_KEY
|
||||
quicommit config set-api-key YOUR_API_KEY
|
||||
|
||||
# Configure Anthropic Claude
|
||||
quicommit config set-llm anthropic
|
||||
quicommit config set-anthropic-key YOUR_API_KEY
|
||||
quicommit config set-api-key YOUR_API_KEY
|
||||
|
||||
# Configure Kimi (Moonshot AI)
|
||||
quicommit config set-llm kimi
|
||||
quicommit config set-kimi-key YOUR_API_KEY
|
||||
quicommit config set-kimi --base-url https://api.moonshot.cn/v1 --model moonshot-v1-8k
|
||||
quicommit config set-api-key YOUR_API_KEY
|
||||
quicommit config set-llm kimi --base-url https://api.moonshot.cn/v1 --model moonshot-v1-8k
|
||||
|
||||
# Configure DeepSeek
|
||||
quicommit config set-llm deepseek
|
||||
quicommit config set-deepseek-key YOUR_API_KEY
|
||||
quicommit config set-deepseek --base-url https://api.deepseek.com/v1 --model deepseek-chat
|
||||
quicommit config set-api-key YOUR_API_KEY
|
||||
quicommit config set-llm deepseek --base-url https://api.deepseek.com/v1 --model deepseek-chat
|
||||
|
||||
# Configure OpenRouter
|
||||
quicommit config set-llm openrouter
|
||||
quicommit config set-openrouter-key YOUR_API_KEY
|
||||
quicommit config set-openrouter --base-url https://openrouter.ai/api/v1 --model openai/gpt-4
|
||||
quicommit config set-api-key YOUR_API_KEY
|
||||
quicommit config set-llm openrouter --base-url https://openrouter.ai/api/v1 --model openai/gpt-4
|
||||
|
||||
# Set commit format
|
||||
quicommit config set-commit-format conventional
|
||||
@@ -193,7 +199,7 @@ quicommit config set-version-prefix v
|
||||
# Set changelog path
|
||||
quicommit config set-changelog-path CHANGELOG.md
|
||||
|
||||
# Set output language
|
||||
# Set output language (en, zh, ja, ko, es, fr, de)
|
||||
quicommit config set-language en
|
||||
|
||||
# Set keep commit types in English
|
||||
@@ -205,8 +211,22 @@ quicommit config set-keep-changelog-types-english true
|
||||
# Test LLM connection
|
||||
quicommit config test-llm
|
||||
|
||||
# Check keyring availability
|
||||
quicommit config check-keyring
|
||||
|
||||
# Show config file path
|
||||
quicommit config path
|
||||
|
||||
# Export configuration (with optional encryption)
|
||||
quicommit config export -o config-backup.toml
|
||||
quicommit config export -o config-backup.enc --password
|
||||
|
||||
# Import configuration
|
||||
quicommit config import -i config-backup.toml
|
||||
quicommit config import -i config-backup.enc --password
|
||||
|
||||
# Reset configuration to defaults
|
||||
quicommit config reset
|
||||
quicommit config reset --force
|
||||
```
|
||||
|
||||
## Command Reference
|
||||
@@ -396,17 +416,31 @@ quicommit config set llm.provider ollama
|
||||
# Get configuration value
|
||||
quicommit config get llm.provider
|
||||
|
||||
# Set API key (stored in system keyring)
|
||||
quicommit config set-api-key YOUR_API_KEY
|
||||
|
||||
# Delete API key from keyring
|
||||
quicommit config delete-api-key
|
||||
|
||||
# Test LLM connection
|
||||
quicommit config test-llm
|
||||
|
||||
# List available models
|
||||
quicommit config list-models
|
||||
|
||||
# Export configuration
|
||||
# Check keyring availability
|
||||
quicommit config check-keyring
|
||||
|
||||
# Show config file path
|
||||
quicommit config path
|
||||
|
||||
# Export configuration (with optional encryption)
|
||||
quicommit config export -o config-backup.toml
|
||||
quicommit config export -o config-backup.enc --password
|
||||
|
||||
# Import configuration
|
||||
quicommit config import -i config-backup.toml
|
||||
quicommit config import -i config-backup.enc --password
|
||||
|
||||
# Reset configuration
|
||||
quicommit config reset --force
|
||||
|
||||
@@ -4,6 +4,13 @@
|
||||
# - macOS: ~/Library/Application Support/quicommit/config.toml
|
||||
# - Windows: %APPDATA%\quicommit\config.toml
|
||||
|
||||
# ⚠️ IMPORTANT: Keyring Feature Update
|
||||
# QuiCommit now uses system keyring to store API keys securely.
|
||||
# This change may cause breaking changes to your existing configuration.
|
||||
# If you encounter issues after updating, please reset your configuration:
|
||||
# quicommit config reset --force
|
||||
# Then reconfigure your settings using the CLI commands.
|
||||
|
||||
# Configuration version (for migration)
|
||||
version = "1"
|
||||
|
||||
|
||||
59
readme_zh.md
59
readme_zh.md
@@ -6,8 +6,11 @@
|
||||
|
||||
【目前还处在早期开发阶段,依然有一些功能未完善,欢迎反馈和贡献。】
|
||||
|
||||
> ⚠️ **重要提示**:QuiCommit 现在使用系统密钥环(keyring)来安全存储 API 密钥。此更改可能会对现有配置造成破坏性变更。如果在更新后遇到问题,请运行 `quicommit config reset --force` 重置配置,然后重新配置您的设置。
|
||||
|
||||

|
||||

|
||||

|
||||
|
||||
## 主要功能
|
||||
|
||||
@@ -16,8 +19,10 @@
|
||||
- **多配置管理**:为不同场景管理多个Git身份,支持SSH密钥和GPG签名配置
|
||||
- **智能标签管理**:基于语义版本自动检测升级,AI生成标签信息
|
||||
- **变更日志生成**:自动生成Keep a Changelog格式的变更日志
|
||||
- **安全保护**:加密存储敏感数据
|
||||
- **安全保护**:使用系统密钥环进行安全存储
|
||||
- **交互式界面**:美观的CLI界面,支持预览和确认
|
||||
- **多语言支持**:支持7种语言输出(中文、英语、日语、韩语、西班牙语、法语、德语)
|
||||
- **配置导出导入**:备份和恢复配置,支持加密保护
|
||||
|
||||
## 安装
|
||||
|
||||
@@ -159,30 +164,30 @@ quicommit profile token
|
||||
```bash
|
||||
# 配置Ollama(本地)
|
||||
quicommit config set-llm ollama
|
||||
quicommit config set-ollama --url http://localhost:11434 --model llama2
|
||||
quicommit config set-llm ollama --url http://localhost:11434 --model llama2
|
||||
|
||||
# 配置OpenAI
|
||||
quicommit config set-llm openai
|
||||
quicommit config set-openai-key YOUR_API_KEY
|
||||
quicommit config set-api-key YOUR_API_KEY
|
||||
|
||||
# 配置Anthropic Claude
|
||||
quicommit config set-llm anthropic
|
||||
quicommit config set-anthropic-key YOUR_API_KEY
|
||||
quicommit config set-api-key YOUR_API_KEY
|
||||
|
||||
# 配置Kimi
|
||||
quicommit config set-llm kimi
|
||||
quicommit config set-kimi-key YOUR_API_KEY
|
||||
quicommit config set-kimi --base-url https://api.moonshot.cn/v1 --model moonshot-v1-8k
|
||||
quicommit config set-api-key YOUR_API_KEY
|
||||
quicommit config set-llm kimi --base-url https://api.moonshot.cn/v1 --model moonshot-v1-8k
|
||||
|
||||
# 配置DeepSeek
|
||||
quicommit config set-llm deepseek
|
||||
quicommit config set-deepseek-key YOUR_API_KEY
|
||||
quicommit config set-deepseek --base-url https://api.deepseek.com/v1 --model deepseek-chat
|
||||
quicommit config set-api-key YOUR_API_KEY
|
||||
quicommit config set-llm deepseek --base-url https://api.deepseek.com/v1 --model deepseek-chat
|
||||
|
||||
# 配置OpenRouter
|
||||
quicommit config set-llm openrouter
|
||||
quicommit config set-openrouter-key YOUR_API_KEY
|
||||
quicommit config set-openrouter --base-url https://openrouter.ai/api/v1 --model openai/gpt-4
|
||||
quicommit config set-api-key YOUR_API_KEY
|
||||
quicommit config set-llm openrouter --base-url https://openrouter.ai/api/v1 --model openai/gpt-4
|
||||
|
||||
# 设置提交格式
|
||||
quicommit config set-commit-format conventional
|
||||
@@ -193,7 +198,7 @@ quicommit config set-version-prefix v
|
||||
# 设置变更日志路径
|
||||
quicommit config set-changelog-path CHANGELOG.md
|
||||
|
||||
# 设置输出语言
|
||||
# 设置输出语言(zh, en, ja, ko, es, fr, de)
|
||||
quicommit config set-language zh
|
||||
|
||||
# 设置保持提交类型为英文
|
||||
@@ -205,8 +210,22 @@ quicommit config set-keep-changelog-types-english true
|
||||
# 测试LLM连接
|
||||
quicommit config test-llm
|
||||
|
||||
# 检查密钥环可用性
|
||||
quicommit config check-keyring
|
||||
|
||||
# 显示配置文件路径
|
||||
quicommit config path
|
||||
|
||||
# 导出配置(支持加密)
|
||||
quicommit config export -o config-backup.toml
|
||||
quicommit config export -o config-backup.enc --password
|
||||
|
||||
# 导入配置
|
||||
quicommit config import -i config-backup.toml
|
||||
quicommit config import -i config-backup.enc --password
|
||||
|
||||
# 重置配置为默认值
|
||||
quicommit config reset
|
||||
quicommit config reset --force
|
||||
```
|
||||
|
||||
## 命令参考
|
||||
@@ -396,17 +415,31 @@ quicommit config set llm.provider ollama
|
||||
# 获取配置值
|
||||
quicommit config get llm.provider
|
||||
|
||||
# 设置API密钥(存储在系统密钥环中)
|
||||
quicommit config set-api-key YOUR_API_KEY
|
||||
|
||||
# 从密钥环删除API密钥
|
||||
quicommit config delete-api-key
|
||||
|
||||
# 测试LLM连接
|
||||
quicommit config test-llm
|
||||
|
||||
# 列出可用模型
|
||||
quicommit config list-models
|
||||
|
||||
# 导出配置
|
||||
# 检查密钥环可用性
|
||||
quicommit config check-keyring
|
||||
|
||||
# 显示配置文件路径
|
||||
quicommit config path
|
||||
|
||||
# 导出配置(支持加密)
|
||||
quicommit config export -o config-backup.toml
|
||||
quicommit config export -o config-backup.enc --password
|
||||
|
||||
# 导入配置
|
||||
quicommit config import -i config-backup.toml
|
||||
quicommit config import -i config-backup.enc --password
|
||||
|
||||
# 重置配置
|
||||
quicommit config reset --force
|
||||
|
||||
@@ -13,13 +13,14 @@ use crate::i18n::{Messages, translate_changelog_category};
|
||||
|
||||
/// Generate changelog
|
||||
#[derive(Parser)]
|
||||
#[command(disable_version_flag = true, disable_help_flag = false)]
|
||||
pub struct ChangelogCommand {
|
||||
/// Output file path
|
||||
#[arg(short, long)]
|
||||
output: Option<PathBuf>,
|
||||
|
||||
/// Version to generate changelog for
|
||||
#[arg(short, long)]
|
||||
#[arg(long)]
|
||||
version: Option<String>,
|
||||
|
||||
/// Generate from specific tag
|
||||
@@ -38,10 +39,6 @@ pub struct ChangelogCommand {
|
||||
#[arg(short, long)]
|
||||
generate: bool,
|
||||
|
||||
/// Prepend to existing changelog
|
||||
#[arg(short, long)]
|
||||
prepend: bool,
|
||||
|
||||
/// Include commit hashes
|
||||
#[arg(long)]
|
||||
include_hashes: bool,
|
||||
@@ -51,7 +48,7 @@ pub struct ChangelogCommand {
|
||||
include_authors: bool,
|
||||
|
||||
/// Format (keep-a-changelog, github-releases)
|
||||
#[arg(short, long)]
|
||||
#[arg(long)]
|
||||
format: Option<String>,
|
||||
|
||||
/// Dry run (output to stdout)
|
||||
@@ -64,9 +61,13 @@ pub struct ChangelogCommand {
|
||||
}
|
||||
|
||||
impl ChangelogCommand {
|
||||
pub async fn execute(&self) -> Result<()> {
|
||||
pub async fn execute(&self, config_path: Option<PathBuf>) -> Result<()> {
|
||||
let repo = find_repo(std::env::current_dir()?.as_path())?;
|
||||
let manager = ConfigManager::new()?;
|
||||
let manager = if let Some(ref path) = config_path {
|
||||
ConfigManager::with_path(path)?
|
||||
} else {
|
||||
ConfigManager::new()?
|
||||
};
|
||||
let config = manager.config();
|
||||
let language = manager.get_language().unwrap_or(Language::English);
|
||||
let messages = Messages::new(language);
|
||||
@@ -157,13 +158,38 @@ impl ChangelogCommand {
|
||||
}
|
||||
}
|
||||
|
||||
// Write to file
|
||||
if self.prepend && output_path.exists() {
|
||||
// Write to file (always prepend to preserve history)
|
||||
if output_path.exists() {
|
||||
let existing = std::fs::read_to_string(&output_path)?;
|
||||
let new_content = format!("{}\n{}", changelog, existing);
|
||||
let new_content = if existing.is_empty() {
|
||||
format!("{}{}", CHANGELOG_HEADER, changelog)
|
||||
} else if existing.starts_with(CHANGELOG_HEADER) {
|
||||
format!("{}{}", CHANGELOG_HEADER, changelog)
|
||||
} else if existing.starts_with("# Changelog") {
|
||||
let lines: Vec<&str> = existing.lines().collect();
|
||||
let mut header_end = 0;
|
||||
|
||||
for (i, line) in lines.iter().enumerate() {
|
||||
if i == 0 && line.starts_with('#') {
|
||||
header_end = i + 1;
|
||||
} else if line.trim().is_empty() {
|
||||
header_end = i + 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
let header = lines[..header_end].join("\n");
|
||||
let rest = lines[header_end..].join("\n");
|
||||
|
||||
format!("{}\n{}\n{}", header, changelog, rest)
|
||||
} else {
|
||||
format!("{}{}", CHANGELOG_HEADER, changelog)
|
||||
};
|
||||
std::fs::write(&output_path, new_content)?;
|
||||
} else {
|
||||
std::fs::write(&output_path, changelog)?;
|
||||
let content = format!("{}{}", CHANGELOG_HEADER, changelog);
|
||||
std::fs::write(&output_path, content)?;
|
||||
}
|
||||
|
||||
println!("{} {:?}", messages.changelog_written(), output_path);
|
||||
@@ -178,12 +204,11 @@ impl ChangelogCommand {
|
||||
messages: &Messages,
|
||||
) -> Result<String> {
|
||||
let manager = ConfigManager::new()?;
|
||||
let config = manager.config();
|
||||
let language = manager.get_language().unwrap_or(Language::English);
|
||||
|
||||
println!("{}", messages.ai_generating_changelog());
|
||||
|
||||
let generator = ContentGenerator::new(&config.llm).await?;
|
||||
let generator = ContentGenerator::new(&manager).await?;
|
||||
generator.generate_changelog_entry(version, commits, language).await
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ use anyhow::{bail, Context, Result};
|
||||
use clap::Parser;
|
||||
use colored::Colorize;
|
||||
use dialoguer::{Confirm, Input, Select};
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::config::{Language, manager::ConfigManager};
|
||||
use crate::config::CommitFormat;
|
||||
@@ -84,12 +85,16 @@ pub struct CommitCommand {
|
||||
}
|
||||
|
||||
impl CommitCommand {
|
||||
pub async fn execute(&self) -> Result<()> {
|
||||
pub async fn execute(&self, config_path: Option<PathBuf>) -> Result<()> {
|
||||
// Find git repository
|
||||
let repo = find_repo(std::env::current_dir()?.as_path())?;
|
||||
|
||||
// Load configuration
|
||||
let manager = ConfigManager::new()?;
|
||||
let manager = if let Some(ref path) = config_path {
|
||||
ConfigManager::with_path(path)?
|
||||
} else {
|
||||
ConfigManager::new()?
|
||||
};
|
||||
let config = manager.config();
|
||||
let language = manager.get_language().unwrap_or(Language::English);
|
||||
let messages = Messages::new(language);
|
||||
@@ -252,22 +257,17 @@ impl CommitCommand {
|
||||
|
||||
async fn generate_commit(&self, repo: &GitRepo, format: CommitFormat, messages: &Messages) -> Result<String> {
|
||||
let manager = ConfigManager::new()?;
|
||||
let config = manager.config();
|
||||
|
||||
// Check if LLM is configured
|
||||
let generator = ContentGenerator::new(&config.llm).await
|
||||
let generator = ContentGenerator::new(&manager).await
|
||||
.context("Failed to initialize LLM. Use --manual for manual commit.")?;
|
||||
|
||||
println!("{}", messages.ai_analyzing());
|
||||
|
||||
let language_str = &config.language.output_language;
|
||||
let language = Language::from_str(language_str).unwrap_or(Language::English);
|
||||
let language = manager.get_language().unwrap_or(Language::English);
|
||||
|
||||
let generated = if self.yes {
|
||||
// Non-interactive mode: generate directly
|
||||
generator.generate_commit_from_repo(repo, format, language).await?
|
||||
} else {
|
||||
// Interactive mode: allow user to review and regenerate
|
||||
generator.generate_commit_interactive(repo, format, language).await?
|
||||
};
|
||||
|
||||
@@ -350,34 +350,49 @@ impl CommitCommand {
|
||||
.output()?;
|
||||
|
||||
if !output.status.success() {
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
bail!("Failed to amend commit: {}", stderr);
|
||||
|
||||
let error_msg = if stderr.is_empty() {
|
||||
if stdout.is_empty() {
|
||||
"GPG signing failed. Please check:\n\
|
||||
1. GPG signing key is configured (git config --get user.signingkey)\n\
|
||||
2. GPG agent is running\n\
|
||||
3. You can sign commits manually (try: git commit --amend -S)".to_string()
|
||||
} else {
|
||||
stdout.to_string()
|
||||
}
|
||||
} else {
|
||||
stderr.to_string()
|
||||
};
|
||||
|
||||
bail!("Failed to amend commit: {}", error_msg);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
// Helper trait for optional builder methods
|
||||
trait CommitBuilderExt {
|
||||
fn scope_opt(self, scope: Option<String>) -> Self;
|
||||
fn body_opt(self, body: Option<String>) -> Self;
|
||||
}
|
||||
// // Helper trait for optional builder methods
|
||||
// trait CommitBuilderExt {
|
||||
// fn scope_opt(self, scope: Option<String>) -> Self;
|
||||
// fn body_opt(self, body: Option<String>) -> Self;
|
||||
// }
|
||||
|
||||
impl CommitBuilderExt for CommitBuilder {
|
||||
fn scope_opt(self, scope: Option<String>) -> Self {
|
||||
if let Some(s) = scope {
|
||||
self.scope(s)
|
||||
} else {
|
||||
self
|
||||
}
|
||||
}
|
||||
// impl CommitBuilderExt for CommitBuilder {
|
||||
// fn scope_opt(self, scope: Option<String>) -> Self {
|
||||
// if let Some(s) = scope {
|
||||
// self.scope(s)
|
||||
// } else {
|
||||
// self
|
||||
// }
|
||||
// }
|
||||
|
||||
fn body_opt(self, body: Option<String>) -> Self {
|
||||
if let Some(b) = body {
|
||||
self.body(b)
|
||||
} else {
|
||||
self
|
||||
}
|
||||
}
|
||||
}
|
||||
// fn body_opt(self, body: Option<String>) -> Self {
|
||||
// if let Some(b) = body {
|
||||
// self.body(b)
|
||||
// } else {
|
||||
// self
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -2,11 +2,13 @@ use anyhow::Result;
|
||||
use clap::Parser;
|
||||
use colored::Colorize;
|
||||
use dialoguer::{Confirm, Input, Select};
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::config::{GitProfile, Language};
|
||||
use crate::config::manager::ConfigManager;
|
||||
use crate::config::profile::{GpgConfig, SshConfig};
|
||||
use crate::i18n::Messages;
|
||||
use crate::utils::keyring::{get_supported_providers, get_default_model, provider_needs_api_key};
|
||||
use crate::utils::validators::validate_email;
|
||||
|
||||
/// Initialize quicommit configuration
|
||||
@@ -22,14 +24,14 @@ pub struct InitCommand {
|
||||
}
|
||||
|
||||
impl InitCommand {
|
||||
pub async fn execute(&self) -> Result<()> {
|
||||
// Start with English messages for initialization
|
||||
pub async fn execute(&self, config_path: Option<PathBuf>) -> Result<()> {
|
||||
let messages = Messages::new(Language::English);
|
||||
println!("{}", messages.initializing().bold().cyan());
|
||||
|
||||
let config_path = crate::config::AppConfig::default_path()?;
|
||||
let config_path = config_path.unwrap_or_else(|| {
|
||||
crate::config::AppConfig::default_path().unwrap()
|
||||
});
|
||||
|
||||
// Check if config already exists
|
||||
if config_path.exists() && !self.reset {
|
||||
if !self.yes {
|
||||
let overwrite = Confirm::new()
|
||||
@@ -41,26 +43,27 @@ impl InitCommand {
|
||||
println!("{}", "Initialization cancelled.".yellow());
|
||||
return Ok(());
|
||||
}
|
||||
} else {
|
||||
println!("{}", "Configuration already exists. Use --reset to overwrite.".yellow());
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
let mut manager = if self.reset {
|
||||
ConfigManager::new()?
|
||||
} else {
|
||||
ConfigManager::new().or_else(|_| Ok::<_, anyhow::Error>(ConfigManager::default()))?
|
||||
};
|
||||
if let Some(parent) = config_path.parent() {
|
||||
std::fs::create_dir_all(parent)
|
||||
.map_err(|e| anyhow::anyhow!("Failed to create config directory: {}", e))?;
|
||||
}
|
||||
|
||||
let mut manager = ConfigManager::with_path_fresh(&config_path)?;
|
||||
|
||||
if self.yes {
|
||||
// Quick setup with defaults
|
||||
self.quick_setup(&mut manager).await?;
|
||||
} else {
|
||||
// Interactive setup
|
||||
self.interactive_setup(&mut manager).await?;
|
||||
}
|
||||
|
||||
manager.save()?;
|
||||
|
||||
// Get configured language for final messages
|
||||
let language = manager.get_language().unwrap_or(Language::English);
|
||||
let messages = Messages::new(language);
|
||||
|
||||
@@ -75,7 +78,6 @@ impl InitCommand {
|
||||
}
|
||||
|
||||
async fn quick_setup(&self, manager: &mut ConfigManager) -> Result<()> {
|
||||
// Try to get git user info
|
||||
let git_config = git2::Config::open_default()?;
|
||||
|
||||
let user_name = git_config.get_string("user.name").unwrap_or_else(|_| "User".to_string());
|
||||
@@ -90,7 +92,6 @@ impl InitCommand {
|
||||
manager.add_profile("default".to_string(), profile)?;
|
||||
manager.set_default_profile(Some("default".to_string()))?;
|
||||
|
||||
// Set default LLM to Ollama
|
||||
manager.set_llm_provider("ollama".to_string());
|
||||
|
||||
Ok(())
|
||||
@@ -100,7 +101,6 @@ impl InitCommand {
|
||||
let messages = Messages::new(Language::English);
|
||||
println!("\n{}", messages.setup_profile().bold());
|
||||
|
||||
// Language selection
|
||||
println!("\n{}", messages.select_output_language().bold());
|
||||
let languages = vec![
|
||||
Language::English,
|
||||
@@ -120,16 +120,13 @@ impl InitCommand {
|
||||
let selected_language = languages[language_idx];
|
||||
manager.set_output_language(selected_language.to_code().to_string());
|
||||
|
||||
// Update messages to selected language
|
||||
let messages = Messages::new(selected_language);
|
||||
|
||||
// Profile name
|
||||
let profile_name: String = Input::new()
|
||||
.with_prompt(messages.profile_name())
|
||||
.default("personal".to_string())
|
||||
.interact_text()?;
|
||||
|
||||
// User info
|
||||
let git_config = git2::Config::open_default().ok();
|
||||
|
||||
let default_name = git_config.as_ref()
|
||||
@@ -171,7 +168,6 @@ impl InitCommand {
|
||||
None
|
||||
};
|
||||
|
||||
// SSH configuration
|
||||
let setup_ssh = Confirm::new()
|
||||
.with_prompt(messages.configure_ssh())
|
||||
.default(false)
|
||||
@@ -183,7 +179,6 @@ impl InitCommand {
|
||||
None
|
||||
};
|
||||
|
||||
// GPG configuration
|
||||
let setup_gpg = Confirm::new()
|
||||
.with_prompt(messages.configure_gpg())
|
||||
.default(false)
|
||||
@@ -195,7 +190,6 @@ impl InitCommand {
|
||||
None
|
||||
};
|
||||
|
||||
// Create profile
|
||||
let mut profile = GitProfile::new(
|
||||
profile_name.clone(),
|
||||
user_name,
|
||||
@@ -214,9 +208,9 @@ impl InitCommand {
|
||||
manager.add_profile(profile_name.clone(), profile)?;
|
||||
manager.set_default_profile(Some(profile_name))?;
|
||||
|
||||
// LLM provider selection
|
||||
println!("\n{}", messages.select_llm_provider().bold());
|
||||
let providers = vec![
|
||||
|
||||
let provider_display_names = vec![
|
||||
"Ollama (local)",
|
||||
"OpenAI",
|
||||
"Anthropic Claude",
|
||||
@@ -224,49 +218,90 @@ impl InitCommand {
|
||||
"DeepSeek",
|
||||
"OpenRouter"
|
||||
];
|
||||
|
||||
let provider_idx = Select::new()
|
||||
.items(&providers)
|
||||
.items(&provider_display_names)
|
||||
.default(0)
|
||||
.interact()?;
|
||||
|
||||
let provider = match provider_idx {
|
||||
0 => "ollama",
|
||||
1 => "openai",
|
||||
2 => "anthropic",
|
||||
3 => "kimi",
|
||||
4 => "deepseek",
|
||||
5 => "openrouter",
|
||||
_ => "ollama",
|
||||
let providers = get_supported_providers();
|
||||
let provider = providers[provider_idx].to_string();
|
||||
|
||||
let keyring = manager.keyring();
|
||||
let keyring_available = keyring.is_available();
|
||||
|
||||
if !keyring_available {
|
||||
println!("\n{}", "⚠ Keyring is not available on this system.".yellow());
|
||||
println!("{}", keyring.get_status_message().yellow());
|
||||
}
|
||||
|
||||
let api_key = if provider_needs_api_key(&provider) {
|
||||
let env_key = std::env::var("QUICOMMIT_API_KEY")
|
||||
.or_else(|_| std::env::var(format!("QUICOMMIT_{}_API_KEY", provider.to_uppercase())))
|
||||
.ok();
|
||||
|
||||
if let Some(_key) = env_key {
|
||||
println!("\n{} {}", "✓".green(), "Found API key in environment variable.".green());
|
||||
None
|
||||
} else if keyring_available {
|
||||
let prompt = match provider.as_str() {
|
||||
"openai" => messages.openai_api_key(),
|
||||
"anthropic" => messages.anthropic_api_key(),
|
||||
"kimi" => messages.kimi_api_key(),
|
||||
"deepseek" => messages.deepseek_api_key(),
|
||||
"openrouter" => messages.openrouter_api_key(),
|
||||
_ => "API Key",
|
||||
};
|
||||
|
||||
manager.set_llm_provider(provider.to_string());
|
||||
let key: String = Input::new()
|
||||
.with_prompt(prompt)
|
||||
.interact_text()?;
|
||||
Some(key)
|
||||
} else {
|
||||
println!("\n{}", "Please set the QUICOMMIT_API_KEY environment variable.".yellow());
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
// Configure API key if needed
|
||||
if provider == "openai" {
|
||||
let api_key: String = Input::new()
|
||||
.with_prompt(messages.openai_api_key())
|
||||
let default_model = get_default_model(&provider);
|
||||
let model: String = Input::new()
|
||||
.with_prompt("Model name")
|
||||
.default(default_model.to_string())
|
||||
.interact_text()?;
|
||||
manager.set_openai_api_key(api_key);
|
||||
} else if provider == "anthropic" {
|
||||
let api_key: String = Input::new()
|
||||
.with_prompt(messages.anthropic_api_key())
|
||||
|
||||
let base_url: Option<String> = if provider == "ollama" {
|
||||
let url: String = Input::new()
|
||||
.with_prompt("Ollama server URL")
|
||||
.default("http://localhost:11434".to_string())
|
||||
.interact_text()?;
|
||||
manager.set_anthropic_api_key(api_key);
|
||||
} else if provider == "kimi" {
|
||||
let api_key: String = Input::new()
|
||||
.with_prompt(messages.kimi_api_key())
|
||||
Some(url)
|
||||
} else {
|
||||
let use_custom_url = Confirm::new()
|
||||
.with_prompt("Use custom API base URL?")
|
||||
.default(false)
|
||||
.interact()?;
|
||||
|
||||
if use_custom_url {
|
||||
let url: String = Input::new()
|
||||
.with_prompt("Base URL")
|
||||
.interact_text()?;
|
||||
manager.set_kimi_api_key(api_key);
|
||||
} else if provider == "deepseek" {
|
||||
let api_key: String = Input::new()
|
||||
.with_prompt(messages.deepseek_api_key())
|
||||
.interact_text()?;
|
||||
manager.set_deepseek_api_key(api_key);
|
||||
} else if provider == "openrouter" {
|
||||
let api_key: String = Input::new()
|
||||
.with_prompt(messages.openrouter_api_key())
|
||||
.interact_text()?;
|
||||
manager.set_openrouter_api_key(api_key);
|
||||
Some(url)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
};
|
||||
|
||||
manager.set_llm_provider(provider.clone());
|
||||
manager.set_llm_model(model);
|
||||
manager.set_llm_base_url(base_url);
|
||||
|
||||
if let Some(key) = api_key {
|
||||
if provider_needs_api_key(&provider) {
|
||||
manager.set_api_key(&key)?;
|
||||
println!("\n{} {}", "✓".green(), "API key stored securely in system keyring.".green());
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -2,6 +2,7 @@ use anyhow::{bail, Result};
|
||||
use clap::{Parser, Subcommand};
|
||||
use colored::Colorize;
|
||||
use dialoguer::{Confirm, Input, Select};
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::config::manager::ConfigManager;
|
||||
use crate::config::{GitProfile, TokenConfig, TokenType};
|
||||
@@ -123,27 +124,34 @@ enum TokenSubcommand {
|
||||
}
|
||||
|
||||
impl ProfileCommand {
|
||||
pub async fn execute(&self) -> Result<()> {
|
||||
pub async fn execute(&self, config_path: Option<PathBuf>) -> Result<()> {
|
||||
match &self.command {
|
||||
Some(ProfileSubcommand::Add) => self.add_profile().await,
|
||||
Some(ProfileSubcommand::Remove { name }) => self.remove_profile(name).await,
|
||||
Some(ProfileSubcommand::List) => self.list_profiles().await,
|
||||
Some(ProfileSubcommand::Show { name }) => self.show_profile(name.as_deref()).await,
|
||||
Some(ProfileSubcommand::Edit { name }) => self.edit_profile(name).await,
|
||||
Some(ProfileSubcommand::SetDefault { name }) => self.set_default(name).await,
|
||||
Some(ProfileSubcommand::SetRepo { name }) => self.set_repo(name).await,
|
||||
Some(ProfileSubcommand::Apply { name, global }) => self.apply_profile(name.as_deref(), *global).await,
|
||||
Some(ProfileSubcommand::Switch) => self.switch_profile().await,
|
||||
Some(ProfileSubcommand::Copy { from, to }) => self.copy_profile(from, to).await,
|
||||
Some(ProfileSubcommand::Token { token_command }) => self.handle_token_command(token_command).await,
|
||||
Some(ProfileSubcommand::Check { name }) => self.check_profile(name.as_deref()).await,
|
||||
Some(ProfileSubcommand::Stats { name }) => self.show_stats(name.as_deref()).await,
|
||||
None => self.list_profiles().await,
|
||||
Some(ProfileSubcommand::Add) => self.add_profile(&config_path).await,
|
||||
Some(ProfileSubcommand::Remove { name }) => self.remove_profile(name, &config_path).await,
|
||||
Some(ProfileSubcommand::List) => self.list_profiles(&config_path).await,
|
||||
Some(ProfileSubcommand::Show { name }) => self.show_profile(name.as_deref(), &config_path).await,
|
||||
Some(ProfileSubcommand::Edit { name }) => self.edit_profile(name, &config_path).await,
|
||||
Some(ProfileSubcommand::SetDefault { name }) => self.set_default(name, &config_path).await,
|
||||
Some(ProfileSubcommand::SetRepo { name }) => self.set_repo(name, &config_path).await,
|
||||
Some(ProfileSubcommand::Apply { name, global }) => self.apply_profile(name.as_deref(), *global, &config_path).await,
|
||||
Some(ProfileSubcommand::Switch) => self.switch_profile(&config_path).await,
|
||||
Some(ProfileSubcommand::Copy { from, to }) => self.copy_profile(from, to, &config_path).await,
|
||||
Some(ProfileSubcommand::Token { token_command }) => self.handle_token_command(token_command, &config_path).await,
|
||||
Some(ProfileSubcommand::Check { name }) => self.check_profile(name.as_deref(), &config_path).await,
|
||||
Some(ProfileSubcommand::Stats { name }) => self.show_stats(name.as_deref(), &config_path).await,
|
||||
None => self.list_profiles(&config_path).await,
|
||||
}
|
||||
}
|
||||
|
||||
async fn add_profile(&self) -> Result<()> {
|
||||
let mut manager = ConfigManager::new()?;
|
||||
fn get_manager(&self, config_path: &Option<PathBuf>) -> Result<ConfigManager> {
|
||||
match config_path {
|
||||
Some(path) => ConfigManager::with_path(path),
|
||||
None => ConfigManager::new(),
|
||||
}
|
||||
}
|
||||
|
||||
async fn add_profile(&self, config_path: &Option<PathBuf>) -> Result<()> {
|
||||
let mut manager = self.get_manager(config_path)?;
|
||||
|
||||
println!("{}", "\nAdd new profile".bold());
|
||||
println!("{}", "─".repeat(40));
|
||||
@@ -220,7 +228,7 @@ impl ProfileCommand {
|
||||
.interact()?;
|
||||
|
||||
if setup_token {
|
||||
self.setup_token_interactive(&mut profile).await?;
|
||||
self.setup_token_interactive(&mut profile, &manager).await?;
|
||||
}
|
||||
|
||||
manager.add_profile(name.clone(), profile)?;
|
||||
@@ -244,8 +252,8 @@ impl ProfileCommand {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn remove_profile(&self, name: &str) -> Result<()> {
|
||||
let mut manager = ConfigManager::new()?;
|
||||
async fn remove_profile(&self, name: &str, config_path: &Option<PathBuf>) -> Result<()> {
|
||||
let mut manager = self.get_manager(config_path)?;
|
||||
|
||||
if !manager.has_profile(name) {
|
||||
bail!("Profile '{}' not found", name);
|
||||
@@ -261,16 +269,18 @@ impl ProfileCommand {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
manager.delete_all_pats_for_profile(name)?;
|
||||
|
||||
manager.remove_profile(name)?;
|
||||
manager.save()?;
|
||||
|
||||
println!("{} Profile '{}' removed", "✓".green(), name);
|
||||
println!("{} Profile '{}' removed (including all stored tokens)", "✓".green(), name);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn list_profiles(&self) -> Result<()> {
|
||||
let manager = ConfigManager::new()?;
|
||||
async fn list_profiles(&self, config_path: &Option<PathBuf>) -> Result<()> {
|
||||
let manager = self.get_manager(config_path)?;
|
||||
|
||||
let profiles = manager.list_profiles();
|
||||
|
||||
@@ -319,19 +329,174 @@ impl ProfileCommand {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn show_profile(&self, name: Option<&str>) -> Result<()> {
|
||||
let manager = ConfigManager::new()?;
|
||||
async fn show_profile(&self, name: Option<&str>, config_path: &Option<PathBuf>) -> Result<()> {
|
||||
let manager = self.get_manager(config_path)?;
|
||||
|
||||
let profile = if let Some(n) = name {
|
||||
manager.get_profile(n)
|
||||
.ok_or_else(|| anyhow::anyhow!("Profile '{}' not found", n))?
|
||||
match find_repo(std::env::current_dir()?.as_path()) {
|
||||
Ok(repo) => {
|
||||
self.show_repo_status(&repo, &manager, name).await
|
||||
}
|
||||
Err(_) => {
|
||||
self.show_global_status(&manager, name).await
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn show_repo_status(&self, repo: &crate::git::GitRepo, manager: &ConfigManager, name: Option<&str>) -> Result<()> {
|
||||
use crate::git::MergedUserConfig;
|
||||
|
||||
let merged_config = MergedUserConfig::from_repo(repo.inner())?;
|
||||
let repo_path = repo.path().to_string_lossy().to_string();
|
||||
|
||||
println!("{}", "\n📁 Current Repository Status".bold());
|
||||
println!("{}", "─".repeat(60));
|
||||
println!("Repository: {}", repo_path.cyan());
|
||||
|
||||
println!("\n{}", "Git User Configuration (merged local/global):".bold());
|
||||
println!("{}", "─".repeat(60));
|
||||
|
||||
self.print_config_entry("User name", &merged_config.name);
|
||||
self.print_config_entry("User email", &merged_config.email);
|
||||
self.print_config_entry("Signing key", &merged_config.signing_key);
|
||||
self.print_config_entry("SSH command", &merged_config.ssh_command);
|
||||
self.print_config_entry("Commit GPG sign", &merged_config.commit_gpgsign);
|
||||
self.print_config_entry("Tag GPG sign", &merged_config.tag_gpgsign);
|
||||
|
||||
let user_name = merged_config.name.value.clone().unwrap_or_default();
|
||||
let user_email = merged_config.email.value.clone().unwrap_or_default();
|
||||
let signing_key = merged_config.signing_key.value.as_deref();
|
||||
|
||||
let matching_profile = manager.find_matching_profile(&user_name, &user_email, signing_key);
|
||||
let repo_profile_name = manager.get_repo_profile_name(&repo_path);
|
||||
|
||||
println!("\n{}", "QuiCommit Profile Status:".bold());
|
||||
println!("{}", "─".repeat(60));
|
||||
|
||||
match (&matching_profile, repo_profile_name) {
|
||||
(Some(profile), Some(mapped_name)) => {
|
||||
if profile.name == *mapped_name {
|
||||
println!("{} Profile '{}' is mapped to this repository", "✓".green(), profile.name.cyan());
|
||||
println!(" This repository's git config matches the saved profile.");
|
||||
} else {
|
||||
manager.default_profile()
|
||||
.ok_or_else(|| anyhow::anyhow!("No default profile set"))?
|
||||
println!("{} Profile '{}' matches current config", "✓".green(), profile.name.cyan());
|
||||
println!(" But repository is mapped to different profile: {}", mapped_name.yellow());
|
||||
}
|
||||
}
|
||||
(Some(profile), None) => {
|
||||
println!("{} Profile '{}' matches current config", "✓".green(), profile.name.cyan());
|
||||
println!(" {} This repository is not mapped to any profile.", "ℹ".yellow());
|
||||
}
|
||||
(None, Some(mapped_name)) => {
|
||||
println!("{} Repository is mapped to profile '{}'", "⚠".yellow(), mapped_name.cyan());
|
||||
println!(" But current git config does not match this profile!");
|
||||
|
||||
if let Some(mapped_profile) = manager.get_profile(mapped_name) {
|
||||
println!("\n Mapped profile config:");
|
||||
println!(" user.name: {}", mapped_profile.user_name);
|
||||
println!(" user.email: {}", mapped_profile.user_email);
|
||||
}
|
||||
}
|
||||
(None, None) => {
|
||||
println!("{} No matching profile found in QuiCommit", "✗".red());
|
||||
println!(" Current git identity is not saved as a QuiCommit profile.");
|
||||
|
||||
let partial_matches = manager.find_partial_matches(&user_name, &user_email);
|
||||
if !partial_matches.is_empty() {
|
||||
println!("\n {} Similar profiles exist:", "ℹ".yellow());
|
||||
for p in partial_matches {
|
||||
let same_name = p.user_name == user_name;
|
||||
let same_email = p.user_email == user_email;
|
||||
let reason = match (same_name, same_email) {
|
||||
(true, true) => "same name & email",
|
||||
(true, false) => "same name",
|
||||
(false, true) => "same email",
|
||||
(false, false) => "partial match",
|
||||
};
|
||||
println!(" - {} ({})", p.name.cyan(), reason.dimmed());
|
||||
}
|
||||
}
|
||||
|
||||
if merged_config.is_complete() {
|
||||
println!("\n {} Would you like to save this identity as a new profile?", "💡".yellow());
|
||||
let save = Confirm::new()
|
||||
.with_prompt("Save current git identity as new profile?")
|
||||
.default(true)
|
||||
.interact()?;
|
||||
|
||||
if save {
|
||||
self.save_current_identity_as_profile(&merged_config, manager).await?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(profile_name) = name {
|
||||
if let Some(profile) = manager.get_profile(profile_name) {
|
||||
println!("\n{}", format!("Requested Profile: {}", profile_name).bold());
|
||||
println!("{}", "─".repeat(60));
|
||||
self.print_profile_details(profile);
|
||||
} else {
|
||||
println!("\n{} Profile '{}' not found", "✗".red(), profile_name);
|
||||
}
|
||||
} else if let Some(profile) = manager.default_profile() {
|
||||
println!("\n{}", format!("Default Profile: {}", profile.name).bold());
|
||||
println!("{}", "─".repeat(60));
|
||||
self.print_profile_details(profile);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn show_global_status(&self, manager: &ConfigManager, name: Option<&str>) -> Result<()> {
|
||||
println!("{}", "\n⚠ Not in a Git Repository".bold().yellow());
|
||||
println!("{}", "─".repeat(60));
|
||||
println!("Run this command inside a git repository to see local config status.");
|
||||
println!();
|
||||
|
||||
if let Some(profile_name) = name {
|
||||
if let Some(profile) = manager.get_profile(profile_name) {
|
||||
println!("{}", format!("Profile: {}", profile_name).bold());
|
||||
println!("{}", "─".repeat(40));
|
||||
self.print_profile_details(profile);
|
||||
} else {
|
||||
bail!("Profile '{}' not found", profile_name);
|
||||
}
|
||||
} else if let Some(profile) = manager.default_profile() {
|
||||
println!("{}", format!("Default Profile: {}", profile.name).bold());
|
||||
println!("{}", "─".repeat(40));
|
||||
self.print_profile_details(profile);
|
||||
} else {
|
||||
println!("{}", "No default profile set.".yellow());
|
||||
println!("Run {} to create one.", "quicommit profile add".cyan());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn print_config_entry(&self, label: &str, entry: &crate::git::ConfigEntry) {
|
||||
use crate::git::ConfigSource;
|
||||
|
||||
let source_indicator = match entry.source {
|
||||
ConfigSource::Local => format!("[{}]", "local".green()),
|
||||
ConfigSource::Global => format!("[{}]", "global".blue()),
|
||||
ConfigSource::NotSet => format!("[{}]", "not set".dimmed()),
|
||||
};
|
||||
|
||||
println!("{}", format!("\nProfile: {}", profile.name).bold());
|
||||
println!("{}", "─".repeat(40));
|
||||
match &entry.value {
|
||||
Some(value) => {
|
||||
println!("{} {}: {}", source_indicator, label, value);
|
||||
if entry.local_value.is_some() && entry.global_value.is_some() {
|
||||
println!(" {} local: {}", "├".dimmed(), entry.local_value.as_ref().unwrap());
|
||||
println!(" {} global: {}", "└".dimmed(), entry.global_value.as_ref().unwrap());
|
||||
}
|
||||
}
|
||||
None => {
|
||||
println!("{} {}: {}", source_indicator, label, "<not set>".dimmed());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn print_profile_details(&self, profile: &GitProfile) {
|
||||
println!("User name: {}", profile.user_name);
|
||||
println!("User email: {}", profile.user_email);
|
||||
|
||||
@@ -376,12 +541,118 @@ impl ProfileCommand {
|
||||
println!(" Last used: {}", last_used);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn save_current_identity_as_profile(&self, merged_config: &crate::git::MergedUserConfig, manager: &ConfigManager) -> Result<()> {
|
||||
let config_path = manager.path().to_path_buf();
|
||||
let mut manager = ConfigManager::with_path(&config_path)?;
|
||||
|
||||
let user_name = merged_config.name.value.clone().unwrap_or_default();
|
||||
let user_email = merged_config.email.value.clone().unwrap_or_default();
|
||||
|
||||
println!("\n{}", "Save New Profile".bold());
|
||||
println!("{}", "─".repeat(40));
|
||||
|
||||
let default_name = user_name.to_lowercase().replace(' ', "-");
|
||||
let profile_name: String = Input::new()
|
||||
.with_prompt("Profile name")
|
||||
.default(default_name)
|
||||
.validate_with(|input: &String| {
|
||||
validate_profile_name(input).map_err(|e| e.to_string())
|
||||
})
|
||||
.interact_text()?;
|
||||
|
||||
if manager.has_profile(&profile_name) {
|
||||
let overwrite = Confirm::new()
|
||||
.with_prompt(&format!("Profile '{}' already exists. Overwrite?", profile_name))
|
||||
.default(false)
|
||||
.interact()?;
|
||||
if !overwrite {
|
||||
println!("{}", "Cancelled.".yellow());
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
let description: String = Input::new()
|
||||
.with_prompt("Description (optional)")
|
||||
.default(format!("Imported from existing git config"))
|
||||
.allow_empty(true)
|
||||
.interact_text()?;
|
||||
|
||||
let is_work = Confirm::new()
|
||||
.with_prompt("Is this a work profile?")
|
||||
.default(false)
|
||||
.interact()?;
|
||||
|
||||
let organization = if is_work {
|
||||
Some(Input::new()
|
||||
.with_prompt("Organization")
|
||||
.interact_text()?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let mut profile = GitProfile::new(profile_name.clone(), user_name, user_email);
|
||||
if !description.is_empty() {
|
||||
profile.description = Some(description);
|
||||
}
|
||||
profile.is_work = is_work;
|
||||
profile.organization = organization;
|
||||
|
||||
if let Some(ref key) = merged_config.signing_key.value {
|
||||
profile.signing_key = Some(key.clone());
|
||||
|
||||
let setup_gpg = Confirm::new()
|
||||
.with_prompt("Configure GPG signing details?")
|
||||
.default(true)
|
||||
.interact()?;
|
||||
|
||||
if setup_gpg {
|
||||
profile.gpg = Some(self.setup_gpg_interactive().await?);
|
||||
}
|
||||
}
|
||||
|
||||
if merged_config.ssh_command.is_set() {
|
||||
let setup_ssh = Confirm::new()
|
||||
.with_prompt("Configure SSH key details?")
|
||||
.default(false)
|
||||
.interact()?;
|
||||
|
||||
if setup_ssh {
|
||||
profile.ssh = Some(self.setup_ssh_interactive().await?);
|
||||
}
|
||||
}
|
||||
|
||||
let setup_token = Confirm::new()
|
||||
.with_prompt("Add a Personal Access Token?")
|
||||
.default(false)
|
||||
.interact()?;
|
||||
|
||||
if setup_token {
|
||||
self.setup_token_interactive(&mut profile, &manager).await?;
|
||||
}
|
||||
|
||||
manager.add_profile(profile_name.clone(), profile)?;
|
||||
manager.save()?;
|
||||
|
||||
println!("{} Profile '{}' saved successfully", "✓".green(), profile_name.cyan());
|
||||
|
||||
let set_default = Confirm::new()
|
||||
.with_prompt("Set as default profile?")
|
||||
.default(true)
|
||||
.interact()?;
|
||||
|
||||
if set_default {
|
||||
manager.set_default_profile(Some(profile_name.clone()))?;
|
||||
manager.save()?;
|
||||
println!("{} Set '{}' as default profile", "✓".green(), profile_name.cyan());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn edit_profile(&self, name: &str) -> Result<()> {
|
||||
let mut manager = ConfigManager::new()?;
|
||||
async fn edit_profile(&self, name: &str, config_path: &Option<PathBuf>) -> Result<()> {
|
||||
let mut manager = self.get_manager(config_path)?;
|
||||
|
||||
let profile = manager.get_profile(name)
|
||||
.ok_or_else(|| anyhow::anyhow!("Profile '{}' not found", name))?
|
||||
@@ -420,8 +691,8 @@ impl ProfileCommand {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn set_default(&self, name: &str) -> Result<()> {
|
||||
let mut manager = ConfigManager::new()?;
|
||||
async fn set_default(&self, name: &str, config_path: &Option<PathBuf>) -> Result<()> {
|
||||
let mut manager = self.get_manager(config_path)?;
|
||||
|
||||
manager.set_default_profile(Some(name.to_string()))?;
|
||||
manager.save()?;
|
||||
@@ -431,22 +702,30 @@ impl ProfileCommand {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn set_repo(&self, name: &str) -> Result<()> {
|
||||
let mut manager = ConfigManager::new()?;
|
||||
async fn set_repo(&self, name: &str, config_path: &Option<PathBuf>) -> Result<()> {
|
||||
let mut manager = self.get_manager(config_path)?;
|
||||
let repo = find_repo(std::env::current_dir()?.as_path())?;
|
||||
|
||||
let repo_path = repo.path().to_string_lossy().to_string();
|
||||
|
||||
manager.set_repo_profile(repo_path, name.to_string())?;
|
||||
manager.set_repo_profile(repo_path.clone(), name.to_string())?;
|
||||
|
||||
// Get the profile and apply it to the repository
|
||||
let profile = manager.get_profile(name)
|
||||
.ok_or_else(|| anyhow::anyhow!("Profile '{}' not found", name))?;
|
||||
|
||||
profile.apply_to_repo(repo.inner())?;
|
||||
manager.record_profile_usage(name, Some(repo_path))?;
|
||||
manager.save()?;
|
||||
|
||||
println!("{} Set '{}' for current repository", "✓".green(), name.cyan());
|
||||
println!("{} Applied profile '{}' to current repository", "✓".green(), name.cyan());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn apply_profile(&self, name: Option<&str>, global: bool) -> Result<()> {
|
||||
let mut manager = ConfigManager::new()?;
|
||||
async fn apply_profile(&self, name: Option<&str>, global: bool, config_path: &Option<PathBuf>) -> Result<()> {
|
||||
let mut manager = self.get_manager(config_path)?;
|
||||
|
||||
let profile_name = if let Some(n) = name {
|
||||
n.to_string()
|
||||
@@ -482,8 +761,8 @@ impl ProfileCommand {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn switch_profile(&self) -> Result<()> {
|
||||
let mut manager = ConfigManager::new()?;
|
||||
async fn switch_profile(&self, config_path: &Option<PathBuf>) -> Result<()> {
|
||||
let mut manager = self.get_manager(config_path)?;
|
||||
|
||||
let profiles: Vec<String> = manager.list_profiles()
|
||||
.into_iter()
|
||||
@@ -519,15 +798,15 @@ impl ProfileCommand {
|
||||
.interact()?;
|
||||
|
||||
if apply {
|
||||
self.apply_profile(Some(selected), false).await?;
|
||||
self.apply_profile(Some(selected), false, config_path).await?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn copy_profile(&self, from: &str, to: &str) -> Result<()> {
|
||||
let mut manager = ConfigManager::new()?;
|
||||
async fn copy_profile(&self, from: &str, to: &str, config_path: &Option<PathBuf>) -> Result<()> {
|
||||
let mut manager = self.get_manager(config_path)?;
|
||||
|
||||
let source = manager.get_profile(from)
|
||||
.ok_or_else(|| anyhow::anyhow!("Profile '{}' not found", from))?
|
||||
@@ -547,21 +826,25 @@ impl ProfileCommand {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn handle_token_command(&self, cmd: &TokenSubcommand) -> Result<()> {
|
||||
async fn handle_token_command(&self, cmd: &TokenSubcommand, config_path: &Option<PathBuf>) -> Result<()> {
|
||||
match cmd {
|
||||
TokenSubcommand::Add { profile, service } => self.add_token(profile, service).await,
|
||||
TokenSubcommand::Remove { profile, service } => self.remove_token(profile, service).await,
|
||||
TokenSubcommand::List { profile } => self.list_tokens(profile).await,
|
||||
TokenSubcommand::Add { profile, service } => self.add_token(profile, service, config_path).await,
|
||||
TokenSubcommand::Remove { profile, service } => self.remove_token(profile, service, config_path).await,
|
||||
TokenSubcommand::List { profile } => self.list_tokens(profile, config_path).await,
|
||||
}
|
||||
}
|
||||
|
||||
async fn add_token(&self, profile_name: &str, service: &str) -> Result<()> {
|
||||
let mut manager = ConfigManager::new()?;
|
||||
async fn add_token(&self, profile_name: &str, service: &str, config_path: &Option<PathBuf>) -> Result<()> {
|
||||
let mut manager = self.get_manager(config_path)?;
|
||||
|
||||
if !manager.has_profile(profile_name) {
|
||||
bail!("Profile '{}' not found", profile_name);
|
||||
}
|
||||
|
||||
if !manager.keyring().is_available() {
|
||||
bail!("Keyring is not available. Cannot store PAT securely. Please ensure your system keyring is accessible.");
|
||||
}
|
||||
|
||||
println!("{}", format!("\nAdd token to profile '{}'", profile_name).bold());
|
||||
println!("{}", "─".repeat(40));
|
||||
|
||||
@@ -589,21 +872,23 @@ impl ProfileCommand {
|
||||
.allow_empty(true)
|
||||
.interact_text()?;
|
||||
|
||||
let mut token = TokenConfig::new(token_value, token_type);
|
||||
let mut token = TokenConfig::new(token_type);
|
||||
if !description.is_empty() {
|
||||
token.description = Some(description);
|
||||
}
|
||||
|
||||
manager.store_pat_for_profile(profile_name, service, &token_value)?;
|
||||
|
||||
manager.add_token_to_profile(profile_name, service.to_string(), token)?;
|
||||
manager.save()?;
|
||||
|
||||
println!("{} Token for '{}' added to profile '{}'", "✓".green(), service.cyan(), profile_name);
|
||||
println!("{} Token for '{}' added to profile '{}' (stored securely in keyring)", "✓".green(), service.cyan(), profile_name);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn remove_token(&self, profile_name: &str, service: &str) -> Result<()> {
|
||||
let mut manager = ConfigManager::new()?;
|
||||
async fn remove_token(&self, profile_name: &str, service: &str, config_path: &Option<PathBuf>) -> Result<()> {
|
||||
let mut manager = self.get_manager(config_path)?;
|
||||
|
||||
if !manager.has_profile(profile_name) {
|
||||
bail!("Profile '{}' not found", profile_name);
|
||||
@@ -622,13 +907,13 @@ impl ProfileCommand {
|
||||
manager.remove_token_from_profile(profile_name, service)?;
|
||||
manager.save()?;
|
||||
|
||||
println!("{} Token '{}' removed from profile '{}'", "✓".green(), service, profile_name);
|
||||
println!("{} Token '{}' removed from profile '{}' (deleted from keyring)", "✓".green(), service, profile_name);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn list_tokens(&self, profile_name: &str) -> Result<()> {
|
||||
let manager = ConfigManager::new()?;
|
||||
async fn list_tokens(&self, profile_name: &str, config_path: &Option<PathBuf>) -> Result<()> {
|
||||
let manager = self.get_manager(config_path)?;
|
||||
|
||||
let profile = manager.get_profile(profile_name)
|
||||
.ok_or_else(|| anyhow::anyhow!("Profile '{}' not found", profile_name))?;
|
||||
@@ -642,7 +927,14 @@ impl ProfileCommand {
|
||||
println!("{}", "─".repeat(40));
|
||||
|
||||
for (service, token) in &profile.tokens {
|
||||
println!("{} ({})", service.cyan().bold(), token.token_type);
|
||||
let has_token = manager.has_pat_for_profile(profile_name, service);
|
||||
let status = if has_token {
|
||||
format!("[{}]", "stored".green())
|
||||
} else {
|
||||
format!("[{}]", "not stored".yellow())
|
||||
};
|
||||
|
||||
println!("{} {} ({})", service.cyan().bold(), status, token.token_type);
|
||||
if let Some(ref desc) = token.description {
|
||||
println!(" {}", desc);
|
||||
}
|
||||
@@ -654,8 +946,8 @@ impl ProfileCommand {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn check_profile(&self, name: Option<&str>) -> Result<()> {
|
||||
let manager = ConfigManager::new()?;
|
||||
async fn check_profile(&self, name: Option<&str>, config_path: &Option<PathBuf>) -> Result<()> {
|
||||
let manager = self.get_manager(config_path)?;
|
||||
|
||||
let profile_name = if let Some(n) = name {
|
||||
n.to_string()
|
||||
@@ -687,8 +979,8 @@ impl ProfileCommand {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn show_stats(&self, name: Option<&str>) -> Result<()> {
|
||||
let manager = ConfigManager::new()?;
|
||||
async fn show_stats(&self, name: Option<&str>, config_path: &Option<PathBuf>) -> Result<()> {
|
||||
let manager = self.get_manager(config_path)?;
|
||||
|
||||
if let Some(n) = name {
|
||||
let profile = manager.get_profile(n)
|
||||
@@ -769,7 +1061,18 @@ impl ProfileCommand {
|
||||
})
|
||||
}
|
||||
|
||||
async fn setup_token_interactive(&self, profile: &mut GitProfile) -> Result<()> {
|
||||
async fn setup_token_interactive(&self, profile: &mut GitProfile, manager: &ConfigManager) -> Result<()> {
|
||||
if !manager.keyring().is_available() {
|
||||
println!("{} Keyring is not available. Cannot store PAT securely.", "⚠".yellow());
|
||||
let continue_anyway = Confirm::new()
|
||||
.with_prompt("Continue without secure token storage?")
|
||||
.default(false)
|
||||
.interact()?;
|
||||
if !continue_anyway {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
let service: String = Input::new()
|
||||
.with_prompt("Service name (e.g., github, gitlab)")
|
||||
.interact_text()?;
|
||||
@@ -778,7 +1081,13 @@ impl ProfileCommand {
|
||||
.with_prompt("Token value")
|
||||
.interact_text()?;
|
||||
|
||||
let token = TokenConfig::new(token_value, TokenType::Personal);
|
||||
let token = TokenConfig::new(TokenType::Personal);
|
||||
|
||||
if manager.keyring().is_available() {
|
||||
manager.store_pat_for_profile(&profile.name, &service, &token_value)?;
|
||||
println!("{} Token stored securely in keyring", "✓".green());
|
||||
}
|
||||
|
||||
profile.add_token(service, token);
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -3,6 +3,7 @@ use clap::Parser;
|
||||
use colored::Colorize;
|
||||
use dialoguer::{Confirm, Input, Select};
|
||||
use semver::Version;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::config::{Language, manager::ConfigManager};
|
||||
use crate::git::{find_repo, GitRepo};
|
||||
@@ -61,9 +62,13 @@ pub struct TagCommand {
|
||||
}
|
||||
|
||||
impl TagCommand {
|
||||
pub async fn execute(&self) -> Result<()> {
|
||||
pub async fn execute(&self, config_path: Option<PathBuf>) -> Result<()> {
|
||||
let repo = find_repo(std::env::current_dir()?.as_path())?;
|
||||
let manager = ConfigManager::new()?;
|
||||
let manager = if let Some(ref path) = config_path {
|
||||
ConfigManager::with_path(path)?
|
||||
} else {
|
||||
ConfigManager::new()?
|
||||
};
|
||||
let config = manager.config();
|
||||
let language = manager.get_language().unwrap_or(Language::English);
|
||||
let messages = Messages::new(language);
|
||||
@@ -265,10 +270,8 @@ impl TagCommand {
|
||||
|
||||
async fn generate_tag_message(&self, repo: &GitRepo, version: &str, messages: &Messages) -> Result<String> {
|
||||
let manager = ConfigManager::new()?;
|
||||
let config = manager.config();
|
||||
let language = manager.get_language().unwrap_or(Language::English);
|
||||
|
||||
// Get commits since last tag
|
||||
let tags = repo.get_tags()?;
|
||||
let commits = if let Some(latest_tag) = tags.first() {
|
||||
repo.get_commits_between(&latest_tag.name, "HEAD")?
|
||||
@@ -282,7 +285,7 @@ impl TagCommand {
|
||||
|
||||
println!("{}", messages.ai_generating_tag(commits.len()));
|
||||
|
||||
let generator = ContentGenerator::new(&config.llm).await?;
|
||||
let generator = ContentGenerator::new(&manager).await?;
|
||||
generator.generate_tag_message(version, &commits, language).await
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use super::{AppConfig, GitProfile, TokenConfig};
|
||||
use crate::utils::keyring::{KeyringManager, get_default_base_url, get_default_model, provider_needs_api_key};
|
||||
use anyhow::{bail, Context, Result};
|
||||
use std::collections::HashMap;
|
||||
// use std::collections::HashMap;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
/// Configuration manager
|
||||
@@ -8,6 +9,7 @@ pub struct ConfigManager {
|
||||
config: AppConfig,
|
||||
config_path: PathBuf,
|
||||
modified: bool,
|
||||
keyring: KeyringManager,
|
||||
}
|
||||
|
||||
impl ConfigManager {
|
||||
@@ -19,11 +21,26 @@ impl ConfigManager {
|
||||
|
||||
/// Create config manager with specific path
|
||||
pub fn with_path(path: &Path) -> Result<Self> {
|
||||
let config = AppConfig::load(path)?;
|
||||
let config = if path.exists() {
|
||||
AppConfig::load(path)?
|
||||
} else {
|
||||
AppConfig::default()
|
||||
};
|
||||
Ok(Self {
|
||||
config,
|
||||
config_path: path.to_path_buf(),
|
||||
modified: false,
|
||||
keyring: KeyringManager::new(),
|
||||
})
|
||||
}
|
||||
|
||||
/// Create config manager with fresh config (ignoring existing)
|
||||
pub fn with_path_fresh(path: &Path) -> Result<Self> {
|
||||
Ok(Self {
|
||||
config: AppConfig::default(),
|
||||
config_path: path.to_path_buf(),
|
||||
modified: true,
|
||||
keyring: KeyringManager::new(),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -47,10 +64,10 @@ impl ConfigManager {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Force save configuration
|
||||
pub fn force_save(&self) -> Result<()> {
|
||||
self.config.save(&self.config_path)
|
||||
}
|
||||
// /// Force save configuration
|
||||
// pub fn force_save(&self) -> Result<()> {
|
||||
// self.config.save(&self.config_path)
|
||||
// }
|
||||
|
||||
/// Get configuration file path
|
||||
pub fn path(&self) -> &Path {
|
||||
@@ -101,11 +118,11 @@ impl ConfigManager {
|
||||
self.config.profiles.get(name)
|
||||
}
|
||||
|
||||
/// Get mutable profile
|
||||
pub fn get_profile_mut(&mut self, name: &str) -> Option<&mut GitProfile> {
|
||||
self.modified = true;
|
||||
self.config.profiles.get_mut(name)
|
||||
}
|
||||
// /// Get mutable profile
|
||||
// pub fn get_profile_mut(&mut self, name: &str) -> Option<&mut GitProfile> {
|
||||
// self.modified = true;
|
||||
// self.config.profiles.get_mut(name)
|
||||
// }
|
||||
|
||||
/// List all profile names
|
||||
pub fn list_profiles(&self) -> Vec<&String> {
|
||||
@@ -153,54 +170,105 @@ impl ConfigManager {
|
||||
}
|
||||
}
|
||||
|
||||
/// Get profile usage statistics
|
||||
pub fn get_profile_usage(&self, name: &str) -> Option<&super::UsageStats> {
|
||||
self.config.profiles.get(name).map(|p| &p.usage)
|
||||
}
|
||||
// /// Get profile usage statistics
|
||||
// pub fn get_profile_usage(&self, name: &str) -> Option<&super::UsageStats> {
|
||||
// self.config.profiles.get(name).map(|p| &p.usage)
|
||||
// }
|
||||
|
||||
// Token management
|
||||
|
||||
/// Add a token to a profile
|
||||
/// Add a token to a profile (stores token in keyring)
|
||||
pub fn add_token_to_profile(&mut self, profile_name: &str, service: String, token: TokenConfig) -> Result<()> {
|
||||
if !self.config.profiles.contains_key(profile_name) {
|
||||
bail!("Profile '{}' does not exist", profile_name);
|
||||
}
|
||||
|
||||
if let Some(profile) = self.config.profiles.get_mut(profile_name) {
|
||||
profile.add_token(service, token);
|
||||
self.modified = true;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Store a PAT token in keyring for a profile
|
||||
pub fn store_pat_for_profile(&self, profile_name: &str, service: &str, token_value: &str) -> Result<()> {
|
||||
let profile = self.get_profile(profile_name)
|
||||
.ok_or_else(|| anyhow::anyhow!("Profile '{}' not found", profile_name))?;
|
||||
|
||||
let user_email = &profile.user_email;
|
||||
|
||||
self.keyring.store_pat(profile_name, user_email, service, token_value)
|
||||
}
|
||||
|
||||
/// Get a PAT token from keyring for a profile
|
||||
pub fn get_pat_for_profile(&self, profile_name: &str, service: &str) -> Result<Option<String>> {
|
||||
let profile = self.get_profile(profile_name)
|
||||
.ok_or_else(|| anyhow::anyhow!("Profile '{}' not found", profile_name))?;
|
||||
|
||||
let user_email = &profile.user_email;
|
||||
|
||||
self.keyring.get_pat(profile_name, user_email, service)
|
||||
}
|
||||
|
||||
/// Check if a PAT token exists for a profile
|
||||
pub fn has_pat_for_profile(&self, profile_name: &str, service: &str) -> bool {
|
||||
if let Some(profile) = self.get_profile(profile_name) {
|
||||
let user_email = &profile.user_email;
|
||||
self.keyring.has_pat(profile_name, user_email, service)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// Remove a token from a profile (deletes from keyring)
|
||||
pub fn remove_token_from_profile(&mut self, profile_name: &str, service: &str) -> Result<()> {
|
||||
if !self.config.profiles.contains_key(profile_name) {
|
||||
bail!("Profile '{}' does not exist", profile_name);
|
||||
}
|
||||
|
||||
let user_email = self.config.profiles.get(profile_name).unwrap().user_email.clone();
|
||||
let services: Vec<String> = self.config.profiles.get(profile_name).unwrap().tokens.keys().cloned().collect();
|
||||
|
||||
if !services.contains(&service.to_string()) {
|
||||
bail!("Token for service '{}' not found in profile '{}'", service, profile_name);
|
||||
}
|
||||
|
||||
/// Get a token from a profile
|
||||
pub fn get_token_from_profile(&self, profile_name: &str, service: &str) -> Option<&TokenConfig> {
|
||||
self.config.profiles.get(profile_name)?.get_token(service)
|
||||
}
|
||||
self.keyring.delete_pat(profile_name, &user_email, service)?;
|
||||
|
||||
/// Remove a token from a profile
|
||||
pub fn remove_token_from_profile(&mut self, profile_name: &str, service: &str) -> Result<()> {
|
||||
if let Some(profile) = self.config.profiles.get_mut(profile_name) {
|
||||
profile.remove_token(service);
|
||||
self.modified = true;
|
||||
Ok(())
|
||||
} else {
|
||||
bail!("Profile '{}' does not exist", profile_name);
|
||||
}
|
||||
}
|
||||
|
||||
/// List all tokens in a profile
|
||||
pub fn list_profile_tokens(&self, profile_name: &str) -> Option<Vec<&String>> {
|
||||
self.config.profiles.get(profile_name).map(|p| p.tokens.keys().collect())
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Delete all PAT tokens for a profile (used when removing a profile)
|
||||
pub fn delete_all_pats_for_profile(&self, profile_name: &str) -> Result<()> {
|
||||
if let Some(profile) = self.get_profile(profile_name) {
|
||||
let user_email = &profile.user_email;
|
||||
let services: Vec<String> = profile.tokens.keys().cloned().collect();
|
||||
|
||||
self.keyring.delete_all_pats_for_profile(profile_name, user_email, &services)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// /// List all tokens in a profile
|
||||
// pub fn list_profile_tokens(&self, profile_name: &str) -> Option<Vec<&String>> {
|
||||
// self.config.profiles.get(profile_name).map(|p| p.tokens.keys().collect())
|
||||
// }
|
||||
|
||||
// Repository profile management
|
||||
|
||||
/// Get profile for repository
|
||||
pub fn get_repo_profile(&self, repo_path: &str) -> Option<&GitProfile> {
|
||||
self.config
|
||||
.repo_profiles
|
||||
.get(repo_path)
|
||||
.and_then(|name| self.config.profiles.get(name))
|
||||
}
|
||||
// /// Get profile for repository
|
||||
// pub fn get_repo_profile(&self, repo_path: &str) -> Option<&GitProfile> {
|
||||
// self.config
|
||||
// .repo_profiles
|
||||
// .get(repo_path)
|
||||
// .and_then(|name| self.config.profiles.get(name))
|
||||
// }
|
||||
|
||||
/// Set profile for repository
|
||||
pub fn set_repo_profile(&mut self, repo_path: String, profile_name: String) -> Result<()> {
|
||||
@@ -212,26 +280,26 @@ impl ConfigManager {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Remove repository profile mapping
|
||||
pub fn remove_repo_profile(&mut self, repo_path: &str) {
|
||||
self.config.repo_profiles.remove(repo_path);
|
||||
self.modified = true;
|
||||
}
|
||||
// /// Remove repository profile mapping
|
||||
// pub fn remove_repo_profile(&mut self, repo_path: &str) {
|
||||
// self.config.repo_profiles.remove(repo_path);
|
||||
// self.modified = true;
|
||||
// }
|
||||
|
||||
/// List repository profile mappings
|
||||
pub fn list_repo_profiles(&self) -> &HashMap<String, String> {
|
||||
&self.config.repo_profiles
|
||||
}
|
||||
// /// List repository profile mappings
|
||||
// pub fn list_repo_profiles(&self) -> &HashMap<String, String> {
|
||||
// &self.config.repo_profiles
|
||||
// }
|
||||
|
||||
/// Get effective profile for a repository (repo-specific -> default)
|
||||
pub fn get_effective_profile(&self, repo_path: Option<&str>) -> Option<&GitProfile> {
|
||||
if let Some(path) = repo_path {
|
||||
if let Some(profile) = self.get_repo_profile(path) {
|
||||
return Some(profile);
|
||||
}
|
||||
}
|
||||
self.default_profile()
|
||||
}
|
||||
// /// Get effective profile for a repository (repo-specific -> default)
|
||||
// pub fn get_effective_profile(&self, repo_path: Option<&str>) -> Option<&GitProfile> {
|
||||
// if let Some(path) = repo_path {
|
||||
// if let Some(profile) = self.get_repo_profile(path) {
|
||||
// return Some(profile);
|
||||
// }
|
||||
// }
|
||||
// self.default_profile()
|
||||
// }
|
||||
|
||||
/// Check and compare profile with git configuration
|
||||
pub fn check_profile_config(&self, profile_name: &str, repo: &git2::Repository) -> Result<super::ProfileComparison> {
|
||||
@@ -240,6 +308,37 @@ impl ConfigManager {
|
||||
profile.compare_with_git_config(repo)
|
||||
}
|
||||
|
||||
/// Find a profile that matches the given user config (name, email, signing_key)
|
||||
pub fn find_matching_profile(&self, user_name: &str, user_email: &str, signing_key: Option<&str>) -> Option<&GitProfile> {
|
||||
for profile in self.config.profiles.values() {
|
||||
let name_match = profile.user_name == user_name;
|
||||
let email_match = profile.user_email == user_email;
|
||||
let key_match = match (signing_key, profile.signing_key()) {
|
||||
(Some(git_key), Some(profile_key)) => git_key == profile_key,
|
||||
(None, None) => true,
|
||||
(Some(_), None) => false,
|
||||
(None, Some(_)) => false,
|
||||
};
|
||||
|
||||
if name_match && email_match && key_match {
|
||||
return Some(profile);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Find profiles that partially match (same name or same email)
|
||||
pub fn find_partial_matches(&self, user_name: &str, user_email: &str) -> Vec<&GitProfile> {
|
||||
self.config.profiles.values()
|
||||
.filter(|p| p.user_name == user_name || p.user_email == user_email)
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Get repo profile mapping
|
||||
pub fn get_repo_profile_name(&self, repo_path: &str) -> Option<&String> {
|
||||
self.config.repo_profiles.get(repo_path)
|
||||
}
|
||||
|
||||
// LLM configuration
|
||||
|
||||
/// Get LLM provider
|
||||
@@ -249,104 +348,148 @@ impl ConfigManager {
|
||||
|
||||
/// Set LLM provider
|
||||
pub fn set_llm_provider(&mut self, provider: String) {
|
||||
self.config.llm.provider = provider;
|
||||
let default_model = get_default_model(&provider);
|
||||
self.config.llm.provider = provider.clone();
|
||||
if self.config.llm.model.is_empty() || self.config.llm.model == "llama2" {
|
||||
self.config.llm.model = default_model.to_string();
|
||||
}
|
||||
self.modified = true;
|
||||
}
|
||||
|
||||
/// Get OpenAI API key
|
||||
pub fn openai_api_key(&self) -> Option<&String> {
|
||||
self.config.llm.openai.api_key.as_ref()
|
||||
/// Get model
|
||||
pub fn llm_model(&self) -> &str {
|
||||
&self.config.llm.model
|
||||
}
|
||||
|
||||
/// Set OpenAI API key
|
||||
pub fn set_openai_api_key(&mut self, key: String) {
|
||||
self.config.llm.openai.api_key = Some(key);
|
||||
/// Set model
|
||||
pub fn set_llm_model(&mut self, model: String) {
|
||||
self.config.llm.model = model;
|
||||
self.modified = true;
|
||||
}
|
||||
|
||||
/// Get Anthropic API key
|
||||
pub fn anthropic_api_key(&self) -> Option<&String> {
|
||||
self.config.llm.anthropic.api_key.as_ref()
|
||||
/// Get base URL (returns provider default if not set)
|
||||
pub fn llm_base_url(&self) -> String {
|
||||
match &self.config.llm.base_url {
|
||||
Some(url) => url.clone(),
|
||||
None => get_default_base_url(&self.config.llm.provider).to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Set Anthropic API key
|
||||
pub fn set_anthropic_api_key(&mut self, key: String) {
|
||||
self.config.llm.anthropic.api_key = Some(key);
|
||||
/// Set base URL
|
||||
pub fn set_llm_base_url(&mut self, url: Option<String>) {
|
||||
self.config.llm.base_url = url;
|
||||
self.modified = true;
|
||||
}
|
||||
|
||||
/// Get Kimi API key
|
||||
pub fn kimi_api_key(&self) -> Option<&String> {
|
||||
self.config.llm.kimi.api_key.as_ref()
|
||||
/// Get API key from configured storage method
|
||||
pub fn get_api_key(&self) -> Option<String> {
|
||||
// First try environment variables (always checked)
|
||||
if let Some(key) = self.keyring.get_api_key(&self.config.llm.provider).unwrap_or(None) {
|
||||
return Some(key);
|
||||
}
|
||||
|
||||
/// Set Kimi API key
|
||||
pub fn set_kimi_api_key(&mut self, key: String) {
|
||||
self.config.llm.kimi.api_key = Some(key);
|
||||
self.modified = true;
|
||||
// Then try config file if configured
|
||||
if self.config.llm.api_key_storage == "config" {
|
||||
return self.config.llm.api_key.clone();
|
||||
}
|
||||
|
||||
/// Get Kimi base URL
|
||||
pub fn kimi_base_url(&self) -> &str {
|
||||
&self.config.llm.kimi.base_url
|
||||
None
|
||||
}
|
||||
|
||||
/// Set Kimi base URL
|
||||
pub fn set_kimi_base_url(&mut self, url: String) {
|
||||
self.config.llm.kimi.base_url = url;
|
||||
self.modified = true;
|
||||
/// Store API key in configured storage method
|
||||
pub fn set_api_key(&self, api_key: &str) -> Result<()> {
|
||||
match self.config.llm.api_key_storage.as_str() {
|
||||
"keyring" => {
|
||||
if !self.keyring.is_available() {
|
||||
bail!("Keyring is not available. Set QUICOMMIT_API_KEY environment variable instead or change api_key_storage to 'config'.");
|
||||
}
|
||||
self.keyring.store_api_key(&self.config.llm.provider, api_key)
|
||||
},
|
||||
"config" => {
|
||||
// We can't modify self.config here since self is immutable
|
||||
// This will be handled by the caller updating the config
|
||||
Ok(())
|
||||
},
|
||||
"environment" => {
|
||||
bail!("API key storage set to 'environment'. Please set QUICOMMIT_{}_API_KEY environment variable.", self.config.llm.provider.to_uppercase());
|
||||
},
|
||||
_ => {
|
||||
bail!("Invalid API key storage method: {}", self.config.llm.api_key_storage);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Get DeepSeek API key
|
||||
pub fn deepseek_api_key(&self) -> Option<&String> {
|
||||
self.config.llm.deepseek.api_key.as_ref()
|
||||
/// Delete API key from configured storage method
|
||||
pub fn delete_api_key(&self) -> Result<()> {
|
||||
match self.config.llm.api_key_storage.as_str() {
|
||||
"keyring" => {
|
||||
if self.keyring.is_available() {
|
||||
self.keyring.delete_api_key(&self.config.llm.provider)?;
|
||||
}
|
||||
},
|
||||
"config" => {
|
||||
// We can't modify self.config here since self is immutable
|
||||
// This will be handled by the caller updating the config
|
||||
},
|
||||
"environment" => {
|
||||
// Environment variables are not managed by the app
|
||||
},
|
||||
_ => {
|
||||
bail!("Invalid API key storage method: {}", self.config.llm.api_key_storage);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Set DeepSeek API key
|
||||
pub fn set_deepseek_api_key(&mut self, key: String) {
|
||||
self.config.llm.deepseek.api_key = Some(key);
|
||||
self.modified = true;
|
||||
/// Check if API key is configured
|
||||
pub fn has_api_key(&self) -> bool {
|
||||
if !provider_needs_api_key(&self.config.llm.provider) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Get DeepSeek base URL
|
||||
pub fn deepseek_base_url(&self) -> &str {
|
||||
&self.config.llm.deepseek.base_url
|
||||
// Check environment variables
|
||||
if self.keyring.get_api_key(&self.config.llm.provider).unwrap_or(None).is_some() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Set DeepSeek base URL
|
||||
pub fn set_deepseek_base_url(&mut self, url: String) {
|
||||
self.config.llm.deepseek.base_url = url;
|
||||
self.modified = true;
|
||||
// Check config file if configured
|
||||
if self.config.llm.api_key_storage == "config" {
|
||||
return self.config.llm.api_key.is_some();
|
||||
}
|
||||
|
||||
/// Get OpenRouter API key
|
||||
pub fn openrouter_api_key(&self) -> Option<&String> {
|
||||
self.config.llm.openrouter.api_key.as_ref()
|
||||
false
|
||||
}
|
||||
|
||||
/// Set OpenRouter API key
|
||||
pub fn set_openrouter_api_key(&mut self, key: String) {
|
||||
self.config.llm.openrouter.api_key = Some(key);
|
||||
self.modified = true;
|
||||
/// Get keyring manager reference
|
||||
pub fn keyring(&self) -> &KeyringManager {
|
||||
&self.keyring
|
||||
}
|
||||
|
||||
/// Get OpenRouter base URL
|
||||
pub fn openrouter_base_url(&self) -> &str {
|
||||
&self.config.llm.openrouter.base_url
|
||||
}
|
||||
// /// Configure LLM provider with all settings
|
||||
// pub fn configure_llm(&mut self, provider: String, model: Option<String>, base_url: Option<String>, api_key: Option<&str>) -> Result<()> {
|
||||
// self.set_llm_provider(provider.clone());
|
||||
|
||||
/// Set OpenRouter base URL
|
||||
pub fn set_openrouter_base_url(&mut self, url: String) {
|
||||
self.config.llm.openrouter.base_url = url;
|
||||
self.modified = true;
|
||||
}
|
||||
// if let Some(m) = model {
|
||||
// self.set_llm_model(m);
|
||||
// }
|
||||
|
||||
// self.set_llm_base_url(base_url);
|
||||
|
||||
// if let Some(key) = api_key {
|
||||
// if provider_needs_api_key(&provider) {
|
||||
// self.set_api_key(key)?;
|
||||
// }
|
||||
// }
|
||||
|
||||
// Ok(())
|
||||
// }
|
||||
|
||||
// Commit configuration
|
||||
|
||||
/// Get commit format
|
||||
pub fn commit_format(&self) -> super::CommitFormat {
|
||||
self.config.commit.format
|
||||
}
|
||||
// /// Get commit format
|
||||
// pub fn commit_format(&self) -> super::CommitFormat {
|
||||
// self.config.commit.format
|
||||
// }
|
||||
|
||||
/// Set commit format
|
||||
pub fn set_commit_format(&mut self, format: super::CommitFormat) {
|
||||
@@ -354,10 +497,10 @@ impl ConfigManager {
|
||||
self.modified = true;
|
||||
}
|
||||
|
||||
/// Check if auto-generate is enabled
|
||||
pub fn auto_generate_commits(&self) -> bool {
|
||||
self.config.commit.auto_generate
|
||||
}
|
||||
// /// Check if auto-generate is enabled
|
||||
// pub fn auto_generate_commits(&self) -> bool {
|
||||
// self.config.commit.auto_generate
|
||||
// }
|
||||
|
||||
/// Set auto-generate commits
|
||||
pub fn set_auto_generate_commits(&mut self, enabled: bool) {
|
||||
@@ -367,10 +510,10 @@ impl ConfigManager {
|
||||
|
||||
// Tag configuration
|
||||
|
||||
/// Get version prefix
|
||||
pub fn version_prefix(&self) -> &str {
|
||||
&self.config.tag.version_prefix
|
||||
}
|
||||
// /// Get version prefix
|
||||
// pub fn version_prefix(&self) -> &str {
|
||||
// &self.config.tag.version_prefix
|
||||
// }
|
||||
|
||||
/// Set version prefix
|
||||
pub fn set_version_prefix(&mut self, prefix: String) {
|
||||
@@ -380,10 +523,10 @@ impl ConfigManager {
|
||||
|
||||
// Changelog configuration
|
||||
|
||||
/// Get changelog path
|
||||
pub fn changelog_path(&self) -> &str {
|
||||
&self.config.changelog.path
|
||||
}
|
||||
// /// Get changelog path
|
||||
// pub fn changelog_path(&self) -> &str {
|
||||
// &self.config.changelog.path
|
||||
// }
|
||||
|
||||
/// Set changelog path
|
||||
pub fn set_changelog_path(&mut self, path: String) {
|
||||
@@ -393,10 +536,10 @@ impl ConfigManager {
|
||||
|
||||
// Language configuration
|
||||
|
||||
/// Get output language
|
||||
pub fn output_language(&self) -> &str {
|
||||
&self.config.language.output_language
|
||||
}
|
||||
// /// Get output language
|
||||
// pub fn output_language(&self) -> &str {
|
||||
// &self.config.language.output_language
|
||||
// }
|
||||
|
||||
/// Set output language
|
||||
pub fn set_output_language(&mut self, language: String) {
|
||||
@@ -458,6 +601,7 @@ impl Default for ConfigManager {
|
||||
config: AppConfig::default(),
|
||||
config_path: PathBuf::new(),
|
||||
modified: false,
|
||||
keyring: KeyringManager::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ pub mod profile;
|
||||
|
||||
pub use profile::{
|
||||
GitProfile, TokenConfig, TokenType,
|
||||
UsageStats, ProfileComparison
|
||||
ProfileComparison
|
||||
};
|
||||
|
||||
/// Application configuration
|
||||
@@ -80,37 +80,16 @@ impl Default for AppConfig {
|
||||
/// LLM configuration
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct LlmConfig {
|
||||
/// Default LLM provider
|
||||
/// Current LLM provider (ollama, openai, anthropic, kimi, deepseek, openrouter)
|
||||
#[serde(default = "default_llm_provider")]
|
||||
pub provider: String,
|
||||
|
||||
/// OpenAI configuration
|
||||
#[serde(default)]
|
||||
pub openai: OpenAiConfig,
|
||||
/// Model to use (stored in config, not in keyring)
|
||||
#[serde(default = "default_model")]
|
||||
pub model: String,
|
||||
|
||||
/// Ollama configuration
|
||||
#[serde(default)]
|
||||
pub ollama: OllamaConfig,
|
||||
|
||||
/// Anthropic Claude configuration
|
||||
#[serde(default)]
|
||||
pub anthropic: AnthropicConfig,
|
||||
|
||||
/// Kimi (Moonshot AI) configuration
|
||||
#[serde(default)]
|
||||
pub kimi: KimiConfig,
|
||||
|
||||
/// DeepSeek configuration
|
||||
#[serde(default)]
|
||||
pub deepseek: DeepSeekConfig,
|
||||
|
||||
/// OpenRouter configuration
|
||||
#[serde(default)]
|
||||
pub openrouter: OpenRouterConfig,
|
||||
|
||||
/// Custom API configuration
|
||||
#[serde(default)]
|
||||
pub custom: Option<CustomLlmConfig>,
|
||||
/// API base URL (optional, will use provider default if not set)
|
||||
pub base_url: Option<String>,
|
||||
|
||||
/// Maximum tokens for generation
|
||||
#[serde(default = "default_max_tokens")]
|
||||
@@ -123,186 +102,35 @@ pub struct LlmConfig {
|
||||
/// Timeout in seconds
|
||||
#[serde(default = "default_timeout")]
|
||||
pub timeout: u64,
|
||||
|
||||
/// API key storage method (keyring, config, environment)
|
||||
#[serde(default = "default_api_key_storage")]
|
||||
pub api_key_storage: String,
|
||||
|
||||
/// API key (stored in config for fallback, encrypted if encrypt_sensitive is true)
|
||||
#[serde(default)]
|
||||
pub api_key: Option<String>,
|
||||
}
|
||||
|
||||
fn default_api_key_storage() -> String {
|
||||
"keyring".to_string()
|
||||
}
|
||||
|
||||
impl Default for LlmConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
provider: default_llm_provider(),
|
||||
openai: OpenAiConfig::default(),
|
||||
ollama: OllamaConfig::default(),
|
||||
anthropic: AnthropicConfig::default(),
|
||||
kimi: KimiConfig::default(),
|
||||
deepseek: DeepSeekConfig::default(),
|
||||
openrouter: OpenRouterConfig::default(),
|
||||
custom: None,
|
||||
model: default_model(),
|
||||
base_url: None,
|
||||
max_tokens: default_max_tokens(),
|
||||
temperature: default_temperature(),
|
||||
timeout: default_timeout(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// OpenAI API configuration
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct OpenAiConfig {
|
||||
/// API key
|
||||
pub api_key: Option<String>,
|
||||
|
||||
/// Model to use
|
||||
#[serde(default = "default_openai_model")]
|
||||
pub model: String,
|
||||
|
||||
/// API base URL (for custom endpoints)
|
||||
#[serde(default = "default_openai_base_url")]
|
||||
pub base_url: String,
|
||||
}
|
||||
|
||||
impl Default for OpenAiConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
api_key_storage: default_api_key_storage(),
|
||||
api_key: None,
|
||||
model: default_openai_model(),
|
||||
base_url: default_openai_base_url(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Ollama configuration
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct OllamaConfig {
|
||||
/// Ollama server URL
|
||||
#[serde(default = "default_ollama_url")]
|
||||
pub url: String,
|
||||
|
||||
/// Model to use
|
||||
#[serde(default = "default_ollama_model")]
|
||||
pub model: String,
|
||||
}
|
||||
|
||||
impl Default for OllamaConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
url: default_ollama_url(),
|
||||
model: default_ollama_model(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Anthropic Claude configuration
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct AnthropicConfig {
|
||||
/// API key
|
||||
pub api_key: Option<String>,
|
||||
|
||||
/// Model to use
|
||||
#[serde(default = "default_anthropic_model")]
|
||||
pub model: String,
|
||||
}
|
||||
|
||||
impl Default for AnthropicConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
api_key: None,
|
||||
model: default_anthropic_model(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Kimi (Moonshot AI) configuration
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct KimiConfig {
|
||||
/// API key
|
||||
pub api_key: Option<String>,
|
||||
|
||||
/// Model to use
|
||||
#[serde(default = "default_kimi_model")]
|
||||
pub model: String,
|
||||
|
||||
/// API base URL (for custom endpoints)
|
||||
#[serde(default = "default_kimi_base_url")]
|
||||
pub base_url: String,
|
||||
}
|
||||
|
||||
impl Default for KimiConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
api_key: None,
|
||||
model: default_kimi_model(),
|
||||
base_url: default_kimi_base_url(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// DeepSeek configuration
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct DeepSeekConfig {
|
||||
/// API key
|
||||
pub api_key: Option<String>,
|
||||
|
||||
/// Model to use
|
||||
#[serde(default = "default_deepseek_model")]
|
||||
pub model: String,
|
||||
|
||||
/// API base URL (for custom endpoints)
|
||||
#[serde(default = "default_deepseek_base_url")]
|
||||
pub base_url: String,
|
||||
}
|
||||
|
||||
impl Default for DeepSeekConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
api_key: None,
|
||||
model: default_deepseek_model(),
|
||||
base_url: default_deepseek_base_url(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// OpenRouter configuration
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct OpenRouterConfig {
|
||||
/// API key
|
||||
pub api_key: Option<String>,
|
||||
|
||||
/// Model to use
|
||||
#[serde(default = "default_openrouter_model")]
|
||||
pub model: String,
|
||||
|
||||
/// API base URL (for custom endpoints)
|
||||
#[serde(default = "default_openrouter_base_url")]
|
||||
pub base_url: String,
|
||||
}
|
||||
|
||||
impl Default for OpenRouterConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
api_key: None,
|
||||
model: default_openrouter_model(),
|
||||
base_url: default_openrouter_base_url(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Custom LLM API configuration
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct CustomLlmConfig {
|
||||
/// API endpoint URL
|
||||
pub url: String,
|
||||
|
||||
/// API key (optional)
|
||||
pub api_key: Option<String>,
|
||||
|
||||
/// Model name
|
||||
pub model: String,
|
||||
|
||||
/// Request format template (JSON)
|
||||
pub request_template: String,
|
||||
|
||||
/// Response path to extract content (e.g., "choices.0.message.content")
|
||||
pub response_path: String,
|
||||
}
|
||||
|
||||
/// Commit configuration
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct CommitConfig {
|
||||
@@ -592,6 +420,10 @@ fn default_llm_provider() -> String {
|
||||
"ollama".to_string()
|
||||
}
|
||||
|
||||
fn default_model() -> String {
|
||||
"llama2".to_string()
|
||||
}
|
||||
|
||||
fn default_max_tokens() -> u32 {
|
||||
500
|
||||
}
|
||||
@@ -604,50 +436,6 @@ fn default_timeout() -> u64 {
|
||||
30
|
||||
}
|
||||
|
||||
fn default_openai_model() -> String {
|
||||
"gpt-4".to_string()
|
||||
}
|
||||
|
||||
fn default_openai_base_url() -> String {
|
||||
"https://api.openai.com/v1".to_string()
|
||||
}
|
||||
|
||||
fn default_ollama_url() -> String {
|
||||
"http://localhost:11434".to_string()
|
||||
}
|
||||
|
||||
fn default_ollama_model() -> String {
|
||||
"llama2".to_string()
|
||||
}
|
||||
|
||||
fn default_anthropic_model() -> String {
|
||||
"claude-3-sonnet-20240229".to_string()
|
||||
}
|
||||
|
||||
fn default_kimi_model() -> String {
|
||||
"moonshot-v1-8k".to_string()
|
||||
}
|
||||
|
||||
fn default_kimi_base_url() -> String {
|
||||
"https://api.moonshot.cn/v1".to_string()
|
||||
}
|
||||
|
||||
fn default_deepseek_model() -> String {
|
||||
"deepseek-chat".to_string()
|
||||
}
|
||||
|
||||
fn default_deepseek_base_url() -> String {
|
||||
"https://api.deepseek.com/v1".to_string()
|
||||
}
|
||||
|
||||
fn default_openrouter_model() -> String {
|
||||
"openai/gpt-3.5-turbo".to_string()
|
||||
}
|
||||
|
||||
fn default_openrouter_base_url() -> String {
|
||||
"https://openrouter.ai/api/v1".to_string()
|
||||
}
|
||||
|
||||
fn default_commit_format() -> CommitFormat {
|
||||
CommitFormat::Conventional
|
||||
}
|
||||
@@ -717,18 +505,70 @@ impl AppConfig {
|
||||
Ok(config_dir.join("quicommit").join("config.toml"))
|
||||
}
|
||||
|
||||
/// Get profile for a repository
|
||||
pub fn get_profile_for_repo(&self, repo_path: &str) -> Option<&GitProfile> {
|
||||
let profile_name = self.repo_profiles.get(repo_path)?;
|
||||
self.profiles.get(profile_name)
|
||||
// /// Get profile for a repository
|
||||
// pub fn get_profile_for_repo(&self, repo_path: &str) -> Option<&GitProfile> {
|
||||
// let profile_name = self.repo_profiles.get(repo_path)?;
|
||||
// self.profiles.get(profile_name)
|
||||
// }
|
||||
|
||||
// /// Set profile for a repository
|
||||
// pub fn set_profile_for_repo(&mut self, repo_path: String, profile_name: String) -> Result<()> {
|
||||
// if !self.profiles.contains_key(&profile_name) {
|
||||
// anyhow::bail!("Profile '{}' does not exist", profile_name);
|
||||
// }
|
||||
// self.repo_profiles.insert(repo_path, profile_name);
|
||||
// Ok(())
|
||||
// }
|
||||
}
|
||||
|
||||
/// Set profile for a repository
|
||||
pub fn set_profile_for_repo(&mut self, repo_path: String, profile_name: String) -> Result<()> {
|
||||
if !self.profiles.contains_key(&profile_name) {
|
||||
anyhow::bail!("Profile '{}' does not exist", profile_name);
|
||||
/// Encrypted PAT data for export
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct EncryptedPat {
|
||||
/// Profile name
|
||||
pub profile_name: String,
|
||||
/// Service name (e.g., github, gitlab)
|
||||
pub service: String,
|
||||
/// User email (for keyring lookup)
|
||||
pub user_email: String,
|
||||
/// Encrypted token value
|
||||
pub encrypted_token: String,
|
||||
}
|
||||
self.repo_profiles.insert(repo_path, profile_name);
|
||||
Ok(())
|
||||
|
||||
/// Export data container with optional encrypted PATs
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ExportData {
|
||||
/// Configuration content (TOML string)
|
||||
pub config: String,
|
||||
/// Encrypted PATs (only present when exporting with encryption)
|
||||
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
||||
pub encrypted_pats: Vec<EncryptedPat>,
|
||||
/// Export version for future compatibility
|
||||
#[serde(default = "default_export_version")]
|
||||
pub export_version: String,
|
||||
}
|
||||
|
||||
fn default_export_version() -> String {
|
||||
"1".to_string()
|
||||
}
|
||||
|
||||
impl ExportData {
|
||||
pub fn new(config: String) -> Self {
|
||||
Self {
|
||||
config,
|
||||
encrypted_pats: Vec::new(),
|
||||
export_version: default_export_version(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_encrypted_pats(config: String, pats: Vec<EncryptedPat>) -> Self {
|
||||
Self {
|
||||
config,
|
||||
encrypted_pats: pats,
|
||||
export_version: default_export_version(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn has_encrypted_pats(&self) -> bool {
|
||||
!self.encrypted_pats.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -177,8 +177,17 @@ impl GitProfile {
|
||||
|
||||
if let Some(ref ssh) = self.ssh {
|
||||
if let Some(ref key_path) = ssh.private_key_path {
|
||||
let path_str = key_path.display().to_string();
|
||||
#[cfg(target_os = "windows")]
|
||||
{
|
||||
config.set_str("core.sshCommand",
|
||||
&format!("ssh -i {}", key_path.display()))?;
|
||||
&format!("ssh -i \"{}\"", path_str.replace('\\', "/")))?;
|
||||
}
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
{
|
||||
config.set_str("core.sshCommand",
|
||||
&format!("ssh -i '{}'", path_str))?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -194,6 +203,30 @@ impl GitProfile {
|
||||
|
||||
if let Some(key) = self.signing_key() {
|
||||
config.set_str("user.signingkey", key)?;
|
||||
|
||||
if self.settings.auto_sign_commits {
|
||||
config.set_bool("commit.gpgsign", true)?;
|
||||
}
|
||||
|
||||
if self.settings.auto_sign_tags {
|
||||
config.set_bool("tag.gpgsign", true)?;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(ref ssh) = self.ssh {
|
||||
if let Some(ref key_path) = ssh.private_key_path {
|
||||
let path_str = key_path.display().to_string();
|
||||
#[cfg(target_os = "windows")]
|
||||
{
|
||||
config.set_str("core.sshCommand",
|
||||
&format!("ssh -i \"{}\"", path_str.replace('\\', "/")))?;
|
||||
}
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
{
|
||||
config.set_str("core.sshCommand",
|
||||
&format!("ssh -i '{}'", path_str))?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -336,7 +369,15 @@ impl SshConfig {
|
||||
if let Some(ref cmd) = self.ssh_command {
|
||||
Some(cmd.clone())
|
||||
} else if let Some(ref key_path) = self.private_key_path {
|
||||
Some(format!("ssh -i '{}'", key_path.display()))
|
||||
let path_str = key_path.display().to_string();
|
||||
#[cfg(target_os = "windows")]
|
||||
{
|
||||
Some(format!("ssh -i \"{}\"", path_str.replace('\\', "/")))
|
||||
}
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
{
|
||||
Some(format!("ssh -i '{}'", path_str))
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
@@ -382,10 +423,6 @@ impl GpgConfig {
|
||||
/// Token configuration for services (GitHub, GitLab, etc.)
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct TokenConfig {
|
||||
/// Token value (encrypted)
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub token: Option<String>,
|
||||
|
||||
/// Token type (personal, oauth, etc.)
|
||||
#[serde(default)]
|
||||
pub token_type: TokenType,
|
||||
@@ -405,25 +442,41 @@ pub struct TokenConfig {
|
||||
/// Description
|
||||
#[serde(default)]
|
||||
pub description: Option<String>,
|
||||
|
||||
/// Indicates if a token is stored in keyring
|
||||
#[serde(default)]
|
||||
pub has_token: bool,
|
||||
}
|
||||
|
||||
impl TokenConfig {
|
||||
/// Create a new token config
|
||||
pub fn new(token: String, token_type: TokenType) -> Self {
|
||||
/// Create a new token config (token stored separately in keyring)
|
||||
pub fn new(token_type: TokenType) -> Self {
|
||||
Self {
|
||||
token: Some(token),
|
||||
token_type,
|
||||
scopes: vec![],
|
||||
expires_at: None,
|
||||
last_used: None,
|
||||
description: None,
|
||||
has_token: true,
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a new token config without token
|
||||
pub fn without_token(token_type: TokenType) -> Self {
|
||||
Self {
|
||||
token_type,
|
||||
scopes: vec![],
|
||||
expires_at: None,
|
||||
last_used: None,
|
||||
description: None,
|
||||
has_token: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Validate token configuration
|
||||
pub fn validate(&self) -> Result<()> {
|
||||
if self.token.is_none() && self.token_type != TokenType::None {
|
||||
bail!("Token value is required for {:?}", self.token_type);
|
||||
if !self.has_token && self.token_type != TokenType::None {
|
||||
bail!("Token is required for {:?}", self.token_type);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -432,6 +485,11 @@ impl TokenConfig {
|
||||
pub fn record_usage(&mut self) {
|
||||
self.last_used = Some(chrono::Utc::now().to_rfc3339());
|
||||
}
|
||||
|
||||
/// Mark that a token is stored
|
||||
pub fn set_has_token(&mut self, has_token: bool) {
|
||||
self.has_token = has_token;
|
||||
}
|
||||
}
|
||||
|
||||
/// Token type
|
||||
@@ -496,8 +554,12 @@ pub struct ConfigDifference {
|
||||
}
|
||||
|
||||
fn default_gpg_program() -> String {
|
||||
if cfg!(target_os = "windows") {
|
||||
"gpg.exe".to_string()
|
||||
} else {
|
||||
"gpg".to_string()
|
||||
}
|
||||
}
|
||||
|
||||
fn default_true() -> bool {
|
||||
true
|
||||
@@ -630,7 +692,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_token_config() {
|
||||
let token = TokenConfig::new("test-token".to_string(), TokenType::Personal);
|
||||
let token = TokenConfig::new(TokenType::Personal);
|
||||
assert!(token.validate().is_ok());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use crate::config::{CommitFormat, LlmConfig, Language};
|
||||
use crate::config::{CommitFormat, Language};
|
||||
use crate::config::manager::ConfigManager;
|
||||
use crate::git::{CommitInfo, GitRepo};
|
||||
use crate::llm::{GeneratedCommit, LlmClient};
|
||||
use anyhow::{Context, Result};
|
||||
use chrono::Utc;
|
||||
|
||||
/// Content generator using LLM
|
||||
pub struct ContentGenerator {
|
||||
@@ -11,12 +11,11 @@ pub struct ContentGenerator {
|
||||
|
||||
impl ContentGenerator {
|
||||
/// Create new content generator
|
||||
pub async fn new(config: &LlmConfig) -> Result<Self> {
|
||||
let llm_client = LlmClient::from_config(config).await?;
|
||||
pub async fn new(manager: &ConfigManager) -> Result<Self> {
|
||||
let llm_client = LlmClient::from_config(manager).await?;
|
||||
|
||||
// Check if provider is available
|
||||
if !llm_client.is_available().await {
|
||||
anyhow::bail!("LLM provider '{}' is not available", config.provider);
|
||||
anyhow::bail!("LLM provider '{}' is not available", manager.llm_provider());
|
||||
}
|
||||
|
||||
Ok(Self { llm_client })
|
||||
@@ -32,7 +31,8 @@ impl ContentGenerator {
|
||||
// Truncate diff if too long
|
||||
let max_diff_len = 4000;
|
||||
let truncated_diff = if diff.len() > max_diff_len {
|
||||
format!("{}\n... (truncated)", &diff[..max_diff_len])
|
||||
let boundary = diff.floor_char_boundary(max_diff_len);
|
||||
format!("{}\n... (truncated)", &diff[..boundary])
|
||||
} else {
|
||||
diff.to_string()
|
||||
};
|
||||
@@ -114,8 +114,7 @@ impl ContentGenerator {
|
||||
format: CommitFormat,
|
||||
language: Language,
|
||||
) -> Result<GeneratedCommit> {
|
||||
use dialoguer::{Confirm, Select};
|
||||
use console::Term;
|
||||
use dialoguer::Select;
|
||||
|
||||
let diff = repo.get_staged_diff()?;
|
||||
|
||||
@@ -145,7 +144,6 @@ impl ContentGenerator {
|
||||
"✓ Accept and commit",
|
||||
"🔄 Regenerate",
|
||||
"✏️ Edit",
|
||||
"📋 Copy to clipboard",
|
||||
"❌ Cancel",
|
||||
];
|
||||
|
||||
@@ -165,24 +163,13 @@ impl ContentGenerator {
|
||||
let edited = crate::utils::editor::edit_content(&generated.to_conventional())?;
|
||||
generated = self.parse_edited_commit(&edited, format)?;
|
||||
}
|
||||
3 => {
|
||||
#[cfg(feature = "clipboard")]
|
||||
{
|
||||
arboard::Clipboard::new()?.set_text(generated.to_conventional())?;
|
||||
println!("Copied to clipboard!");
|
||||
}
|
||||
#[cfg(not(feature = "clipboard"))]
|
||||
{
|
||||
println!("Clipboard feature not enabled");
|
||||
}
|
||||
}
|
||||
4 => anyhow::bail!("Cancelled by user"),
|
||||
3 => anyhow::bail!("Cancelled by user"),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_edited_commit(&self, edited: &str, format: CommitFormat) -> Result<GeneratedCommit> {
|
||||
fn parse_edited_commit(&self, edited: &str, _format: CommitFormat) -> Result<GeneratedCommit> {
|
||||
let parsed = crate::git::commit::parse_commit_message(edited);
|
||||
|
||||
Ok(GeneratedCommit {
|
||||
|
||||
@@ -1,10 +1,19 @@
|
||||
use super::{CommitInfo, GitRepo};
|
||||
use anyhow::{Context, Result};
|
||||
use chrono::{DateTime, TimeZone, Utc};
|
||||
use chrono::{DateTime, Utc};
|
||||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
|
||||
pub const CHANGELOG_HEADER: &str = r#"# Changelog
|
||||
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
"#;
|
||||
|
||||
/// Changelog generator
|
||||
pub struct ChangelogGenerator {
|
||||
format: ChangelogFormat,
|
||||
@@ -109,9 +118,10 @@ impl ChangelogGenerator {
|
||||
};
|
||||
|
||||
let new_content = if existing.is_empty() {
|
||||
format!("# Changelog\n\n{}", entry)
|
||||
} else {
|
||||
// Find position after header
|
||||
format!("{}{}", CHANGELOG_HEADER, entry)
|
||||
} else if existing.starts_with(CHANGELOG_HEADER) {
|
||||
format!("{}{}", CHANGELOG_HEADER, entry)
|
||||
} else if existing.starts_with("# Changelog") {
|
||||
let lines: Vec<&str> = existing.lines().collect();
|
||||
let mut header_end = 0;
|
||||
|
||||
@@ -129,6 +139,8 @@ impl ChangelogGenerator {
|
||||
let rest = lines[header_end..].join("\n");
|
||||
|
||||
format!("{}\n{}\n{}", header, entry, rest)
|
||||
} else {
|
||||
format!("{}{}", CHANGELOG_HEADER, entry)
|
||||
};
|
||||
|
||||
fs::write(changelog_path, new_content)
|
||||
@@ -232,8 +244,6 @@ impl ChangelogGenerator {
|
||||
let mut breaking = vec![];
|
||||
|
||||
for commit in commits {
|
||||
let msg = commit.subject();
|
||||
|
||||
if commit.message.contains("BREAKING CHANGE") {
|
||||
breaking.push(commit);
|
||||
}
|
||||
@@ -380,16 +390,7 @@ pub fn init_changelog(path: &Path) -> Result<()> {
|
||||
anyhow::bail!("Changelog already exists at {:?}", path);
|
||||
}
|
||||
|
||||
let content = r#"# Changelog
|
||||
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
"#;
|
||||
|
||||
fs::write(path, content)
|
||||
fs::write(path, CHANGELOG_HEADER)
|
||||
.with_context(|| format!("Failed to create changelog: {:?}", path))?;
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use super::GitRepo;
|
||||
use anyhow::{bail, Context, Result};
|
||||
use anyhow::{bail, Result};
|
||||
use chrono::Local;
|
||||
|
||||
/// Commit builder for creating commits
|
||||
@@ -47,6 +47,12 @@ impl CommitBuilder {
|
||||
self
|
||||
}
|
||||
|
||||
/// Set scope (optional)
|
||||
pub fn scope_opt(mut self, scope: Option<String>) -> Self {
|
||||
self.scope = scope;
|
||||
self
|
||||
}
|
||||
|
||||
/// Set description
|
||||
pub fn description(mut self, description: impl Into<String>) -> Self {
|
||||
self.description = Some(description.into());
|
||||
@@ -59,6 +65,12 @@ impl CommitBuilder {
|
||||
self
|
||||
}
|
||||
|
||||
/// Set body (optional)
|
||||
pub fn body_opt(mut self, body: Option<String>) -> Self {
|
||||
self.body = body;
|
||||
self
|
||||
}
|
||||
|
||||
/// Set footer
|
||||
pub fn footer(mut self, footer: impl Into<String>) -> Self {
|
||||
self.footer = Some(footer.into());
|
||||
@@ -174,8 +186,23 @@ impl CommitBuilder {
|
||||
.output()?;
|
||||
|
||||
if !output.status.success() {
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
bail!("Failed to amend commit: {}", stderr);
|
||||
|
||||
let error_msg = if stderr.is_empty() {
|
||||
if stdout.is_empty() {
|
||||
"GPG signing failed. Please check:\n\
|
||||
1. GPG signing key is configured (git config --get user.signingkey)\n\
|
||||
2. GPG agent is running\n\
|
||||
3. You can sign commits manually (try: git commit --amend -S)".to_string()
|
||||
} else {
|
||||
stdout.to_string()
|
||||
}
|
||||
} else {
|
||||
stderr.to_string()
|
||||
};
|
||||
|
||||
bail!("Failed to amend commit: {}", error_msg);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
390
src/git/mod.rs
390
src/git/mod.rs
@@ -1,17 +1,173 @@
|
||||
use anyhow::{bail, Context, Result};
|
||||
use git2::{Repository, Signature, StatusOptions, Config, Oid, ObjectType};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::path::{Path, PathBuf, Component};
|
||||
use std::collections::HashMap;
|
||||
use tempfile;
|
||||
|
||||
pub mod changelog;
|
||||
pub mod commit;
|
||||
pub mod tag;
|
||||
|
||||
pub use changelog::ChangelogGenerator;
|
||||
pub use commit::CommitBuilder;
|
||||
pub use tag::TagBuilder;
|
||||
#[cfg(target_os = "windows")]
|
||||
use std::os::windows::ffi::OsStringExt;
|
||||
|
||||
fn normalize_path_for_git2(path: &Path) -> PathBuf {
|
||||
let mut normalized = path.to_path_buf();
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
{
|
||||
let path_str = path.to_string_lossy();
|
||||
if path_str.starts_with(r"\\?\") {
|
||||
if let Some(stripped) = path_str.strip_prefix(r"\\?\") {
|
||||
normalized = PathBuf::from(stripped);
|
||||
}
|
||||
}
|
||||
if path_str.starts_with(r"\\?\UNC\") {
|
||||
if let Some(stripped) = path_str.strip_prefix(r"\\?\UNC\") {
|
||||
normalized = PathBuf::from(format!(r"\\{}", stripped));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
normalized
|
||||
}
|
||||
|
||||
fn get_absolute_path<P: AsRef<Path>>(path: P) -> Result<PathBuf> {
|
||||
let path = path.as_ref();
|
||||
|
||||
if path.is_absolute() {
|
||||
return Ok(normalize_path_for_git2(path));
|
||||
}
|
||||
|
||||
let current_dir = std::env::current_dir()
|
||||
.with_context(|| "Failed to get current directory")?;
|
||||
|
||||
let absolute = current_dir.join(path);
|
||||
Ok(normalize_path_for_git2(&absolute))
|
||||
}
|
||||
|
||||
fn resolve_path_without_canonicalize(path: &Path) -> PathBuf {
|
||||
let mut components = Vec::new();
|
||||
|
||||
for component in path.components() {
|
||||
match component {
|
||||
Component::ParentDir => {
|
||||
if !components.is_empty() && components.last() != Some(&Component::ParentDir) {
|
||||
components.pop();
|
||||
} else {
|
||||
components.push(component);
|
||||
}
|
||||
}
|
||||
Component::CurDir => {}
|
||||
_ => components.push(component),
|
||||
}
|
||||
}
|
||||
|
||||
let mut result = PathBuf::new();
|
||||
for component in components {
|
||||
result.push(component.as_os_str());
|
||||
}
|
||||
|
||||
normalize_path_for_git2(&result)
|
||||
}
|
||||
|
||||
fn try_open_repo_with_git2(path: &Path) -> Result<Repository> {
|
||||
let normalized = normalize_path_for_git2(path);
|
||||
|
||||
let discover_opts = git2::RepositoryOpenFlags::empty();
|
||||
let ceiling_dirs: [&str; 0] = [];
|
||||
|
||||
let repo = Repository::open_ext(&normalized, discover_opts, &ceiling_dirs)
|
||||
.or_else(|_| Repository::discover(&normalized))
|
||||
.or_else(|_| Repository::open(&normalized));
|
||||
|
||||
repo.map_err(|e| anyhow::anyhow!("git2 failed: {}", e))
|
||||
}
|
||||
|
||||
fn try_open_repo_with_git_cli(path: &Path) -> Result<Repository> {
|
||||
let output = std::process::Command::new("git")
|
||||
.args(&["rev-parse", "--show-toplevel"])
|
||||
.current_dir(path)
|
||||
.output()
|
||||
.context("Failed to execute git command")?;
|
||||
|
||||
if !output.status.success() {
|
||||
bail!("git CLI failed to find repository");
|
||||
}
|
||||
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
let git_root = stdout.trim();
|
||||
|
||||
if git_root.is_empty() {
|
||||
bail!("git CLI returned empty path");
|
||||
}
|
||||
|
||||
let git_root_path = PathBuf::from(git_root);
|
||||
let normalized = normalize_path_for_git2(&git_root_path);
|
||||
|
||||
Repository::open(&normalized)
|
||||
.with_context(|| format!("Failed to open repo from git CLI path: {:?}", normalized))
|
||||
}
|
||||
|
||||
fn diagnose_repo_issue(path: &Path) -> String {
|
||||
let mut issues = Vec::new();
|
||||
|
||||
if !path.exists() {
|
||||
issues.push(format!("Path does not exist: {:?}", path));
|
||||
} else if !path.is_dir() {
|
||||
issues.push(format!("Path is not a directory: {:?}", path));
|
||||
}
|
||||
|
||||
let git_dir = path.join(".git");
|
||||
if git_dir.exists() {
|
||||
if git_dir.is_dir() {
|
||||
issues.push("Found .git directory".to_string());
|
||||
let config_file = git_dir.join("config");
|
||||
if config_file.exists() {
|
||||
issues.push("Git config file exists".to_string());
|
||||
} else {
|
||||
issues.push("WARNING: Git config file missing".to_string());
|
||||
}
|
||||
} else {
|
||||
issues.push("Found .git file (submodule or worktree)".to_string());
|
||||
}
|
||||
} else {
|
||||
issues.push("No .git found in current directory".to_string());
|
||||
|
||||
let mut current = path;
|
||||
let mut depth = 0;
|
||||
while let Some(parent) = current.parent() {
|
||||
depth += 1;
|
||||
if depth > 20 {
|
||||
break;
|
||||
}
|
||||
let parent_git = parent.join(".git");
|
||||
if parent_git.exists() {
|
||||
issues.push(format!("Found .git in parent directory: {:?}", parent));
|
||||
break;
|
||||
}
|
||||
current = parent;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
{
|
||||
let path_str = path.to_string_lossy();
|
||||
if path_str.starts_with(r"\\?\") {
|
||||
issues.push("Path has Windows extended-length prefix (\\\\?\\)".to_string());
|
||||
}
|
||||
if path_str.contains('\\') && path_str.contains('/') {
|
||||
issues.push("WARNING: Path has mixed path separators".to_string());
|
||||
}
|
||||
}
|
||||
|
||||
if let Ok(current_dir) = std::env::current_dir() {
|
||||
issues.push(format!("Current working directory: {:?}", current_dir));
|
||||
}
|
||||
|
||||
issues.join("\n ")
|
||||
}
|
||||
|
||||
/// Git repository wrapper with enhanced cross-platform support
|
||||
pub struct GitRepo {
|
||||
repo: Repository,
|
||||
path: PathBuf,
|
||||
@@ -19,28 +175,45 @@ pub struct GitRepo {
|
||||
}
|
||||
|
||||
impl GitRepo {
|
||||
/// Open a git repository
|
||||
pub fn open<P: AsRef<Path>>(path: P) -> Result<Self> {
|
||||
let path = path.as_ref();
|
||||
let absolute_path = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());
|
||||
|
||||
let repo = Repository::discover(&absolute_path)
|
||||
.or_else(|_| Repository::open(&absolute_path))
|
||||
.with_context(|| {
|
||||
format!(
|
||||
"Failed to open git repository at '{:?}'. Please ensure:\n\
|
||||
1. The directory is set as safe (run: git config --global --add safe.directory \"{}\")\n\
|
||||
2. The path is correct and contains a valid '.git' folder.",
|
||||
absolute_path,
|
||||
absolute_path.display()
|
||||
let absolute_path = get_absolute_path(path)?;
|
||||
let resolved_path = resolve_path_without_canonicalize(&absolute_path);
|
||||
|
||||
let repo = try_open_repo_with_git2(&resolved_path)
|
||||
.or_else(|git2_err| {
|
||||
try_open_repo_with_git_cli(&resolved_path)
|
||||
.map_err(|cli_err| {
|
||||
let diagnosis = diagnose_repo_issue(&resolved_path);
|
||||
anyhow::anyhow!(
|
||||
"Failed to open git repository:\n\
|
||||
\n\
|
||||
=== git2 Error ===\n {}\n\
|
||||
\n\
|
||||
=== git CLI Error ===\n {}\n\
|
||||
\n\
|
||||
=== Diagnosis ===\n {}\n\
|
||||
\n\
|
||||
=== Suggestions ===\n\
|
||||
1. Ensure you are inside a git repository\n\
|
||||
2. Run: git status (to verify git works)\n\
|
||||
3. Run: git config --global --add safe.directory \"*\"\n\
|
||||
4. Check file permissions",
|
||||
git2_err, cli_err, diagnosis
|
||||
)
|
||||
})
|
||||
})?;
|
||||
|
||||
let repo_path = repo.workdir()
|
||||
.map(|p| p.to_path_buf())
|
||||
.unwrap_or_else(|| resolved_path.clone());
|
||||
|
||||
let config = repo.config().ok();
|
||||
|
||||
Ok(Self {
|
||||
repo,
|
||||
path: absolute_path,
|
||||
path: normalize_path_for_git2(&repo_path),
|
||||
config,
|
||||
})
|
||||
}
|
||||
@@ -341,19 +514,29 @@ impl GitRepo {
|
||||
let temp_file = tempfile::NamedTempFile::new()?;
|
||||
std::fs::write(temp_file.path(), message)?;
|
||||
|
||||
let mut cmd = std::process::Command::new("git");
|
||||
cmd.args(&["commit", "-S", "-F", temp_file.path().to_str().unwrap()])
|
||||
.current_dir(&self.path)
|
||||
.output()?;
|
||||
|
||||
let output = std::process::Command::new("git")
|
||||
.args(&["commit", "-S", "-F", temp_file.path().to_str().unwrap()])
|
||||
.current_dir(&self.path)
|
||||
.output()?;
|
||||
|
||||
if !output.status.success() {
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
bail!("Failed to create signed commit: {}", stderr);
|
||||
|
||||
let error_msg = if stderr.is_empty() {
|
||||
if stdout.is_empty() {
|
||||
"GPG signing failed. Please check:\n\
|
||||
1. GPG signing key is configured (git config --get user.signingkey)\n\
|
||||
2. GPG agent is running\n\
|
||||
3. You can sign commits manually (try: git commit -S -m 'test')".to_string()
|
||||
} else {
|
||||
stdout.to_string()
|
||||
}
|
||||
} else {
|
||||
stderr.to_string()
|
||||
};
|
||||
|
||||
bail!("Failed to create signed commit: {}", error_msg);
|
||||
}
|
||||
|
||||
let head = self.repo.head()?;
|
||||
@@ -459,12 +642,16 @@ impl GitRepo {
|
||||
name: name.to_string(),
|
||||
target: oid.to_string(),
|
||||
message: commit.message().unwrap_or("").to_string(),
|
||||
time: commit.time().seconds(),
|
||||
});
|
||||
}
|
||||
|
||||
true
|
||||
})?;
|
||||
|
||||
// Sort tags by time (newest first)
|
||||
tags.sort_by(|a, b| b.time.cmp(&a.time));
|
||||
|
||||
Ok(tags)
|
||||
}
|
||||
|
||||
@@ -649,6 +836,7 @@ pub struct TagInfo {
|
||||
pub name: String,
|
||||
pub target: String,
|
||||
pub message: String,
|
||||
pub time: i64,
|
||||
}
|
||||
|
||||
/// Repository status summary
|
||||
@@ -685,23 +873,75 @@ impl StatusSummary {
|
||||
}
|
||||
}
|
||||
|
||||
/// Find git repository starting from path and walking up
|
||||
pub fn find_repo<P: AsRef<Path>>(start_path: P) -> Result<GitRepo> {
|
||||
let start_path = start_path.as_ref();
|
||||
|
||||
if let Ok(repo) = GitRepo::open(start_path) {
|
||||
let absolute_start = get_absolute_path(start_path)?;
|
||||
let resolved_start = resolve_path_without_canonicalize(&absolute_start);
|
||||
|
||||
if let Ok(repo) = GitRepo::open(&resolved_start) {
|
||||
return Ok(repo);
|
||||
}
|
||||
|
||||
let mut current = start_path;
|
||||
let mut current = resolved_start.as_path();
|
||||
let mut attempted_paths = vec![current.to_string_lossy().to_string()];
|
||||
|
||||
let max_depth = 50;
|
||||
let mut depth = 0;
|
||||
|
||||
while let Some(parent) = current.parent() {
|
||||
depth += 1;
|
||||
if depth > max_depth {
|
||||
break;
|
||||
}
|
||||
|
||||
attempted_paths.push(parent.to_string_lossy().to_string());
|
||||
|
||||
if let Ok(repo) = GitRepo::open(parent) {
|
||||
return Ok(repo);
|
||||
}
|
||||
current = parent;
|
||||
}
|
||||
|
||||
bail!("No git repository found starting from {:?}", start_path)
|
||||
if let Ok(output) = std::process::Command::new("git")
|
||||
.args(&["rev-parse", "--show-toplevel"])
|
||||
.current_dir(&resolved_start)
|
||||
.output()
|
||||
{
|
||||
if output.status.success() {
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
let git_root = stdout.trim();
|
||||
if !git_root.is_empty() {
|
||||
if let Ok(repo) = GitRepo::open(git_root) {
|
||||
return Ok(repo);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let diagnosis = diagnose_repo_issue(&resolved_start);
|
||||
|
||||
bail!(
|
||||
"No git repository found.\n\
|
||||
\n\
|
||||
=== Starting Path ===\n {:?}\n\
|
||||
\n\
|
||||
=== Paths Attempted ===\n {}\n\
|
||||
\n\
|
||||
=== Current Directory ===\n {:?}\n\
|
||||
\n\
|
||||
=== Diagnosis ===\n {}\n\
|
||||
\n\
|
||||
=== Suggestions ===\n\
|
||||
1. Ensure you are inside a git repository (run: git status)\n\
|
||||
2. Initialize a new repo: git init\n\
|
||||
3. Clone an existing repo: git clone <url>\n\
|
||||
4. Check if .git directory exists and is accessible",
|
||||
resolved_start,
|
||||
attempted_paths.join("\n "),
|
||||
std::env::current_dir().unwrap_or_default(),
|
||||
diagnosis
|
||||
)
|
||||
}
|
||||
|
||||
/// Check if path is inside a git repository
|
||||
@@ -802,6 +1042,102 @@ impl<'a> GitConfigHelper<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Configuration source indicator
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum ConfigSource {
|
||||
Local,
|
||||
Global,
|
||||
NotSet,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for ConfigSource {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
ConfigSource::Local => write!(f, "local"),
|
||||
ConfigSource::Global => write!(f, "global"),
|
||||
ConfigSource::NotSet => write!(f, "not set"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Single configuration entry with source information
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ConfigEntry {
|
||||
pub value: Option<String>,
|
||||
pub source: ConfigSource,
|
||||
pub local_value: Option<String>,
|
||||
pub global_value: Option<String>,
|
||||
}
|
||||
|
||||
impl ConfigEntry {
|
||||
pub fn new(local: Option<String>, global: Option<String>) -> Self {
|
||||
let (value, source) = match (&local, &global) {
|
||||
(Some(_), _) => (local.clone(), ConfigSource::Local),
|
||||
(None, Some(_)) => (global.clone(), ConfigSource::Global),
|
||||
(None, None) => (None, ConfigSource::NotSet),
|
||||
};
|
||||
Self {
|
||||
value,
|
||||
source,
|
||||
local_value: local,
|
||||
global_value: global,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_set(&self) -> bool {
|
||||
self.value.is_some()
|
||||
}
|
||||
|
||||
pub fn is_local(&self) -> bool {
|
||||
self.source == ConfigSource::Local
|
||||
}
|
||||
|
||||
pub fn is_global(&self) -> bool {
|
||||
self.source == ConfigSource::Global
|
||||
}
|
||||
}
|
||||
|
||||
/// Merged user configuration with local/global source tracking
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct MergedUserConfig {
|
||||
pub name: ConfigEntry,
|
||||
pub email: ConfigEntry,
|
||||
pub signing_key: ConfigEntry,
|
||||
pub ssh_command: ConfigEntry,
|
||||
pub commit_gpgsign: ConfigEntry,
|
||||
pub tag_gpgsign: ConfigEntry,
|
||||
}
|
||||
|
||||
impl MergedUserConfig {
|
||||
pub fn from_repo(repo: &Repository) -> Result<Self> {
|
||||
let local_config = repo.config().ok();
|
||||
let global_config = git2::Config::open_default().ok();
|
||||
|
||||
let get_entry = |key: &str| -> ConfigEntry {
|
||||
let local = local_config.as_ref().and_then(|c| c.get_string(key).ok());
|
||||
let global = global_config.as_ref().and_then(|c| c.get_string(key).ok());
|
||||
ConfigEntry::new(local, global)
|
||||
};
|
||||
|
||||
Ok(Self {
|
||||
name: get_entry("user.name"),
|
||||
email: get_entry("user.email"),
|
||||
signing_key: get_entry("user.signingkey"),
|
||||
ssh_command: get_entry("core.sshCommand"),
|
||||
commit_gpgsign: get_entry("commit.gpgsign"),
|
||||
tag_gpgsign: get_entry("tag.gpgsign"),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn is_complete(&self) -> bool {
|
||||
self.name.is_set() && self.email.is_set()
|
||||
}
|
||||
|
||||
pub fn has_local_overrides(&self) -> bool {
|
||||
self.name.is_local() || self.email.is_local() || self.signing_key.is_local() || self.ssh_command.is_local()
|
||||
}
|
||||
}
|
||||
|
||||
/// User configuration for git
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct UserConfig {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use super::GitRepo;
|
||||
use anyhow::{bail, Context, Result};
|
||||
use anyhow::{bail, Result};
|
||||
use semver::Version;
|
||||
|
||||
/// Tag builder for creating tags
|
||||
@@ -281,8 +281,9 @@ pub fn delete_tag(repo: &GitRepo, name: &str, remote: Option<&str>) -> Result<()
|
||||
if let Some(remote) = remote {
|
||||
use std::process::Command;
|
||||
|
||||
let refspec = format!(":refs/tags/{}", name);
|
||||
let output = Command::new("git")
|
||||
.args(&["push", remote, ":refs/tags/{}"])
|
||||
.args(&["push", remote, &refspec])
|
||||
.current_dir(repo.path())
|
||||
.output()?;
|
||||
|
||||
|
||||
@@ -2,6 +2,4 @@ pub mod messages;
|
||||
pub mod translator;
|
||||
|
||||
pub use messages::Messages;
|
||||
pub use translator::Translator;
|
||||
pub use translator::translate_commit_type;
|
||||
pub use translator::translate_changelog_category;
|
||||
|
||||
@@ -70,6 +70,12 @@ impl AnthropicClient {
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
/// List available models
|
||||
pub async fn list_models(&self) -> Result<Vec<String>> {
|
||||
// Anthropic doesn't have a models API endpoint, return predefined list
|
||||
Ok(ANTHROPIC_MODELS.iter().map(|&m| m.to_string()).collect())
|
||||
}
|
||||
|
||||
/// Validate API key
|
||||
pub async fn validate_key(&self) -> Result<bool> {
|
||||
let url = "https://api.anthropic.com/v1/messages";
|
||||
|
||||
@@ -82,8 +82,8 @@ impl DeepSeekClient {
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
/// Validate API key
|
||||
pub async fn validate_key(&self) -> Result<bool> {
|
||||
/// List available models
|
||||
pub async fn list_models(&self) -> Result<Vec<String>> {
|
||||
let url = format!("{}/models", self.base_url);
|
||||
|
||||
let response = self.client
|
||||
@@ -91,16 +91,44 @@ impl DeepSeekClient {
|
||||
.header("Authorization", format!("Bearer {}", self.api_key))
|
||||
.send()
|
||||
.await
|
||||
.context("Failed to validate DeepSeek API key")?;
|
||||
.context("Failed to list DeepSeek models")?;
|
||||
|
||||
if response.status().is_success() {
|
||||
Ok(true)
|
||||
} else if response.status().as_u16() == 401 {
|
||||
Ok(false)
|
||||
} else {
|
||||
if !response.status().is_success() {
|
||||
let status = response.status();
|
||||
let text = response.text().await.unwrap_or_default();
|
||||
bail!("DeepSeek API error: {} - {}", status, text)
|
||||
bail!("DeepSeek API error: {} - {}", status, text);
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct ModelsResponse {
|
||||
data: Vec<Model>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Model {
|
||||
id: String,
|
||||
}
|
||||
|
||||
let result: ModelsResponse = response
|
||||
.json()
|
||||
.await
|
||||
.context("Failed to parse DeepSeek response")?;
|
||||
|
||||
Ok(result.data.into_iter().map(|m| m.id).collect())
|
||||
}
|
||||
|
||||
/// Validate API key
|
||||
pub async fn validate_key(&self) -> Result<bool> {
|
||||
match self.list_models().await {
|
||||
Ok(_) => Ok(true),
|
||||
Err(e) => {
|
||||
let err_str = e.to_string();
|
||||
if err_str.contains("401") || err_str.contains("Unauthorized") {
|
||||
Ok(false)
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,8 +82,8 @@ impl KimiClient {
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
/// Validate API key
|
||||
pub async fn validate_key(&self) -> Result<bool> {
|
||||
/// List available models
|
||||
pub async fn list_models(&self) -> Result<Vec<String>> {
|
||||
let url = format!("{}/models", self.base_url);
|
||||
|
||||
let response = self.client
|
||||
@@ -91,16 +91,44 @@ impl KimiClient {
|
||||
.header("Authorization", format!("Bearer {}", self.api_key))
|
||||
.send()
|
||||
.await
|
||||
.context("Failed to validate Kimi API key")?;
|
||||
.context("Failed to list Kimi models")?;
|
||||
|
||||
if response.status().is_success() {
|
||||
Ok(true)
|
||||
} else if response.status().as_u16() == 401 {
|
||||
Ok(false)
|
||||
} else {
|
||||
if !response.status().is_success() {
|
||||
let status = response.status();
|
||||
let text = response.text().await.unwrap_or_default();
|
||||
bail!("Kimi API error: {} - {}", status, text)
|
||||
bail!("Kimi API error: {} - {}", status, text);
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct ModelsResponse {
|
||||
data: Vec<Model>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Model {
|
||||
id: String,
|
||||
}
|
||||
|
||||
let result: ModelsResponse = response
|
||||
.json()
|
||||
.await
|
||||
.context("Failed to parse Kimi response")?;
|
||||
|
||||
Ok(result.data.into_iter().map(|m| m.id).collect())
|
||||
}
|
||||
|
||||
/// Validate API key
|
||||
pub async fn validate_key(&self) -> Result<bool> {
|
||||
match self.list_models().await {
|
||||
Ok(_) => Ok(true),
|
||||
Err(e) => {
|
||||
let err_str = e.to_string();
|
||||
if err_str.contains("401") || err_str.contains("Unauthorized") {
|
||||
Ok(false)
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
use anyhow::{bail, Context, Result};
|
||||
use async_trait::async_trait;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::time::Duration;
|
||||
use crate::config::Language;
|
||||
|
||||
@@ -58,48 +57,50 @@ impl Default for LlmClientConfig {
|
||||
}
|
||||
|
||||
impl LlmClient {
|
||||
/// Create LLM client from configuration
|
||||
pub async fn from_config(config: &crate::config::LlmConfig) -> Result<Self> {
|
||||
/// Create LLM client from configuration manager
|
||||
pub async fn from_config(manager: &crate::config::manager::ConfigManager) -> Result<Self> {
|
||||
let config = manager.config();
|
||||
let client_config = LlmClientConfig {
|
||||
max_tokens: config.max_tokens,
|
||||
temperature: config.temperature,
|
||||
timeout: Duration::from_secs(config.timeout),
|
||||
max_tokens: config.llm.max_tokens,
|
||||
temperature: config.llm.temperature,
|
||||
timeout: Duration::from_secs(config.llm.timeout),
|
||||
};
|
||||
|
||||
let provider: Box<dyn LlmProvider> = match config.provider.as_str() {
|
||||
let provider = config.llm.provider.as_str();
|
||||
let model = config.llm.model.as_str();
|
||||
let base_url = manager.llm_base_url();
|
||||
let api_key = manager.get_api_key();
|
||||
|
||||
let provider: Box<dyn LlmProvider> = match provider {
|
||||
"ollama" => {
|
||||
Box::new(OllamaClient::new(&config.ollama.url, &config.ollama.model))
|
||||
Box::new(OllamaClient::new(&base_url, model))
|
||||
}
|
||||
"openai" => {
|
||||
let api_key = config.openai.api_key.as_ref()
|
||||
let key = api_key.as_ref()
|
||||
.ok_or_else(|| anyhow::anyhow!("OpenAI API key not configured"))?;
|
||||
Box::new(OpenAiClient::new(
|
||||
&config.openai.base_url,
|
||||
api_key,
|
||||
&config.openai.model,
|
||||
)?)
|
||||
Box::new(OpenAiClient::new(&base_url, key, model)?)
|
||||
}
|
||||
"anthropic" => {
|
||||
let api_key = config.anthropic.api_key.as_ref()
|
||||
let key = api_key.as_ref()
|
||||
.ok_or_else(|| anyhow::anyhow!("Anthropic API key not configured"))?;
|
||||
Box::new(AnthropicClient::new(api_key, &config.anthropic.model)?)
|
||||
Box::new(AnthropicClient::new(key, model)?)
|
||||
}
|
||||
"kimi" => {
|
||||
let api_key = config.kimi.api_key.as_ref()
|
||||
let key = api_key.as_ref()
|
||||
.ok_or_else(|| anyhow::anyhow!("Kimi API key not configured"))?;
|
||||
Box::new(KimiClient::with_base_url(api_key, &config.kimi.model, &config.kimi.base_url)?)
|
||||
Box::new(KimiClient::with_base_url(key, model, &base_url)?)
|
||||
}
|
||||
"deepseek" => {
|
||||
let api_key = config.deepseek.api_key.as_ref()
|
||||
let key = api_key.as_ref()
|
||||
.ok_or_else(|| anyhow::anyhow!("DeepSeek API key not configured"))?;
|
||||
Box::new(DeepSeekClient::with_base_url(api_key, &config.deepseek.model, &config.deepseek.base_url)?)
|
||||
Box::new(DeepSeekClient::with_base_url(key, model, &base_url)?)
|
||||
}
|
||||
"openrouter" => {
|
||||
let api_key = config.openrouter.api_key.as_ref()
|
||||
let key = api_key.as_ref()
|
||||
.ok_or_else(|| anyhow::anyhow!("OpenRouter API key not configured"))?;
|
||||
Box::new(OpenRouterClient::with_base_url(api_key, &config.openrouter.model, &config.openrouter.base_url)?)
|
||||
Box::new(OpenRouterClient::with_base_url(key, model, &base_url)?)
|
||||
}
|
||||
_ => bail!("Unknown LLM provider: {}", config.provider),
|
||||
_ => bail!("Unknown LLM provider: {}", provider),
|
||||
};
|
||||
|
||||
Ok(Self {
|
||||
@@ -1013,3 +1014,10 @@ Gruppieren Sie Commits nach:
|
||||
|
||||
Formatieren Sie in Markdown mit geeigneten Überschriften und Aufzählungspunkten.
|
||||
"#;
|
||||
|
||||
/// Test LLM connection
|
||||
pub async fn test_connection(manager: &crate::config::manager::ConfigManager) -> Result<String> {
|
||||
let client = LlmClient::from_config(manager).await?;
|
||||
let response = client.provider.generate("Say 'Hello, World!'").await?;
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
@@ -82,8 +82,8 @@ impl OpenRouterClient {
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
/// Validate API key
|
||||
pub async fn validate_key(&self) -> Result<bool> {
|
||||
/// List available models
|
||||
pub async fn list_models(&self) -> Result<Vec<String>> {
|
||||
let url = format!("{}/models", self.base_url);
|
||||
|
||||
let response = self.client
|
||||
@@ -93,16 +93,44 @@ impl OpenRouterClient {
|
||||
.header("X-Title", "QuiCommit")
|
||||
.send()
|
||||
.await
|
||||
.context("Failed to validate OpenRouter API key")?;
|
||||
.context("Failed to list OpenRouter models")?;
|
||||
|
||||
if response.status().is_success() {
|
||||
Ok(true)
|
||||
} else if response.status().as_u16() == 401 {
|
||||
Ok(false)
|
||||
} else {
|
||||
if !response.status().is_success() {
|
||||
let status = response.status();
|
||||
let text = response.text().await.unwrap_or_default();
|
||||
bail!("OpenRouter API error: {} - {}", status, text)
|
||||
bail!("OpenRouter API error: {} - {}", status, text);
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct ModelsResponse {
|
||||
data: Vec<Model>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Model {
|
||||
id: String,
|
||||
}
|
||||
|
||||
let result: ModelsResponse = response
|
||||
.json()
|
||||
.await
|
||||
.context("Failed to parse OpenRouter response")?;
|
||||
|
||||
Ok(result.data.into_iter().map(|m| m.id).collect())
|
||||
}
|
||||
|
||||
/// Validate API key
|
||||
pub async fn validate_key(&self) -> Result<bool> {
|
||||
match self.list_models().await {
|
||||
Ok(_) => Ok(true),
|
||||
Err(e) => {
|
||||
let err_str = e.to_string();
|
||||
if err_str.contains("401") || err_str.contains("Unauthorized") {
|
||||
Ok(false)
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -211,7 +239,7 @@ pub const OPENROUTER_MODELS: &[&str] = &[
|
||||
];
|
||||
|
||||
/// Check if a model name is valid
|
||||
pub fn is_valid_model(model: &str) -> bool {
|
||||
pub fn is_valid_model(_model: &str) -> bool {
|
||||
// Since OpenRouter supports many models, we'll allow any model name
|
||||
// but provide some popular ones as suggestions
|
||||
true
|
||||
|
||||
17
src/main.rs
17
src/main.rs
@@ -1,5 +1,6 @@
|
||||
use anyhow::Result;
|
||||
use clap::{Parser, Subcommand};
|
||||
use std::path::PathBuf;
|
||||
use tracing::debug;
|
||||
|
||||
mod commands;
|
||||
@@ -74,7 +75,6 @@ enum Commands {
|
||||
async fn main() -> Result<()> {
|
||||
let cli = Cli::parse();
|
||||
|
||||
// Initialize logging
|
||||
let log_level = match cli.verbose {
|
||||
0 => "warn",
|
||||
1 => "info",
|
||||
@@ -89,13 +89,14 @@ async fn main() -> Result<()> {
|
||||
|
||||
debug!("Starting quicommit v{}", env!("CARGO_PKG_VERSION"));
|
||||
|
||||
// Execute command
|
||||
let config_path: Option<PathBuf> = cli.config.map(PathBuf::from);
|
||||
|
||||
match cli.command {
|
||||
Commands::Init(cmd) => cmd.execute().await,
|
||||
Commands::Commit(cmd) => cmd.execute().await,
|
||||
Commands::Tag(cmd) => cmd.execute().await,
|
||||
Commands::Changelog(cmd) => cmd.execute().await,
|
||||
Commands::Profile(cmd) => cmd.execute().await,
|
||||
Commands::Config(cmd) => cmd.execute().await,
|
||||
Commands::Init(cmd) => cmd.execute(config_path).await,
|
||||
Commands::Commit(cmd) => cmd.execute(config_path).await,
|
||||
Commands::Tag(cmd) => cmd.execute(config_path).await,
|
||||
Commands::Changelog(cmd) => cmd.execute(config_path).await,
|
||||
Commands::Profile(cmd) => cmd.execute(config_path).await,
|
||||
Commands::Config(cmd) => cmd.execute(config_path).await,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,8 +41,22 @@ pub fn get_editor() -> String {
|
||||
.or_else(|_| std::env::var("VISUAL"))
|
||||
.unwrap_or_else(|_| {
|
||||
if cfg!(target_os = "windows") {
|
||||
if let Ok(code) = which::which("code") {
|
||||
return "code --wait".to_string();
|
||||
}
|
||||
if let Ok(notepad) = which::which("notepad") {
|
||||
return "notepad".to_string();
|
||||
}
|
||||
"notepad".to_string()
|
||||
} else if cfg!(target_os = "macos") {
|
||||
if which::which("code").is_ok() {
|
||||
return "code --wait".to_string();
|
||||
}
|
||||
"vi".to_string()
|
||||
} else {
|
||||
if which::which("nano").is_ok() {
|
||||
return "nano".to_string();
|
||||
}
|
||||
"vi".to_string()
|
||||
}
|
||||
})
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
use chrono::{DateTime, Local, Utc};
|
||||
use regex::Regex;
|
||||
|
||||
/// Format commit message with conventional commit format
|
||||
@@ -12,7 +11,6 @@ pub fn format_conventional_commit(
|
||||
) -> String {
|
||||
let mut message = String::new();
|
||||
|
||||
// Type and scope
|
||||
message.push_str(commit_type);
|
||||
if let Some(s) = scope {
|
||||
message.push_str(&format!("({})", s));
|
||||
@@ -22,12 +20,10 @@ pub fn format_conventional_commit(
|
||||
}
|
||||
message.push_str(&format!(": {}", description));
|
||||
|
||||
// Body
|
||||
if let Some(b) = body {
|
||||
message.push_str(&format!("\n\n{}", b));
|
||||
}
|
||||
|
||||
// Footer
|
||||
if let Some(f) = footer {
|
||||
message.push_str(&format!("\n\n{}", f));
|
||||
}
|
||||
@@ -46,26 +42,22 @@ pub fn format_commitlint_commit(
|
||||
) -> String {
|
||||
let mut message = String::new();
|
||||
|
||||
// Header
|
||||
message.push_str(commit_type);
|
||||
if let Some(s) = scope {
|
||||
message.push_str(&format!("({})", s));
|
||||
}
|
||||
message.push_str(&format!(": {}", subject));
|
||||
|
||||
// References
|
||||
if let Some(refs) = references {
|
||||
for reference in refs {
|
||||
message.push_str(&format!(" #{}", reference));
|
||||
}
|
||||
}
|
||||
|
||||
// Body
|
||||
if let Some(b) = body {
|
||||
message.push_str(&format!("\n\n{}", b));
|
||||
}
|
||||
|
||||
// Footer
|
||||
if let Some(f) = footer {
|
||||
message.push_str(&format!("\n\n{}", f));
|
||||
}
|
||||
@@ -73,38 +65,11 @@ pub fn format_commitlint_commit(
|
||||
message
|
||||
}
|
||||
|
||||
/// Format date for commit message
|
||||
pub fn format_commit_date(date: &DateTime<Local>) -> String {
|
||||
date.format("%Y-%m-%d %H:%M:%S").to_string()
|
||||
}
|
||||
|
||||
/// Format date for changelog
|
||||
pub fn format_changelog_date(date: &DateTime<Utc>) -> String {
|
||||
date.format("%Y-%m-%d").to_string()
|
||||
}
|
||||
|
||||
/// Format tag name with version
|
||||
pub fn format_tag_name(version: &str, prefix: Option<&str>) -> String {
|
||||
match prefix {
|
||||
Some(p) => format!("{}{}", p, version),
|
||||
None => version.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Wrap text at specified width
|
||||
pub fn wrap_text(text: &str, width: usize) -> String {
|
||||
textwrap::fill(text, width)
|
||||
}
|
||||
|
||||
/// Truncate text with ellipsis
|
||||
pub fn truncate(text: &str, max_len: usize) -> String {
|
||||
if text.len() <= max_len {
|
||||
text.to_string()
|
||||
} else {
|
||||
format!("{}...", &text[..max_len.saturating_sub(3)])
|
||||
}
|
||||
}
|
||||
|
||||
/// Clean commit message (remove comments, extra whitespace)
|
||||
pub fn clean_message(message: &str) -> String {
|
||||
let comment_regex = Regex::new(r"^#.*$").unwrap();
|
||||
@@ -118,44 +83,6 @@ pub fn clean_message(message: &str) -> String {
|
||||
.to_string()
|
||||
}
|
||||
|
||||
/// Format list as markdown bullet points
|
||||
pub fn format_markdown_list(items: &[String]) -> String {
|
||||
items
|
||||
.iter()
|
||||
.map(|item| format!("- {}", item))
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n")
|
||||
}
|
||||
|
||||
/// Format changelog section
|
||||
pub fn format_changelog_section(
|
||||
version: &str,
|
||||
date: &str,
|
||||
changes: &[(String, Vec<String>)],
|
||||
) -> String {
|
||||
let mut section = format!("## [{}] - {}\n\n", version, date);
|
||||
|
||||
for (category, items) in changes {
|
||||
if !items.is_empty() {
|
||||
section.push_str(&format!("### {}\n\n", category));
|
||||
for item in items {
|
||||
section.push_str(&format!("- {}\n", item));
|
||||
}
|
||||
section.push('\n');
|
||||
}
|
||||
}
|
||||
|
||||
section
|
||||
}
|
||||
|
||||
/// Format git config key
|
||||
pub fn format_git_config_key(section: &str, subsection: Option<&str>, key: &str) -> String {
|
||||
match subsection {
|
||||
Some(sub) => format!("{}.{}.{}", section, sub, key),
|
||||
None => format!("{}.{}", section, key),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -189,10 +116,4 @@ mod tests {
|
||||
|
||||
assert!(msg.starts_with("feat!: change API response format"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_truncate() {
|
||||
assert_eq!(truncate("hello", 10), "hello");
|
||||
assert_eq!(truncate("hello world", 8), "hello...");
|
||||
}
|
||||
}
|
||||
|
||||
297
src/utils/keyring.rs
Normal file
297
src/utils/keyring.rs
Normal file
@@ -0,0 +1,297 @@
|
||||
use anyhow::{bail, Context, Result};
|
||||
use std::env;
|
||||
|
||||
const SERVICE_NAME: &str = "quicommit";
|
||||
const ENV_API_KEY: &str = "QUICOMMIT_API_KEY";
|
||||
|
||||
const PAT_SERVICE_PREFIX: &str = "quicommit/pat";
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum KeyringStatus {
|
||||
Available,
|
||||
Unavailable,
|
||||
}
|
||||
|
||||
pub struct KeyringManager {
|
||||
status: KeyringStatus,
|
||||
}
|
||||
|
||||
impl KeyringManager {
|
||||
pub fn new() -> Self {
|
||||
let status = Self::check_keyring_availability();
|
||||
Self { status }
|
||||
}
|
||||
|
||||
pub fn check_keyring_availability() -> KeyringStatus {
|
||||
#[cfg(target_os = "windows")]
|
||||
{
|
||||
KeyringStatus::Available
|
||||
}
|
||||
#[cfg(target_os = "macos")]
|
||||
{
|
||||
KeyringStatus::Available
|
||||
}
|
||||
#[cfg(target_os = "linux")]
|
||||
{
|
||||
Self::check_linux_keyring()
|
||||
}
|
||||
#[cfg(not(any(target_os = "windows", target_os = "macos", target_os = "linux")))]
|
||||
{
|
||||
KeyringStatus::Unavailable
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
fn check_linux_keyring() -> KeyringStatus {
|
||||
use std::path::Path;
|
||||
|
||||
let has_dbus = Path::new("/usr/bin/dbus-daemon").exists()
|
||||
|| Path::new("/bin/dbus-daemon").exists()
|
||||
|| env::var("DBUS_SESSION_BUS_ADDRESS").is_ok();
|
||||
|
||||
let has_keyring = Path::new("/usr/bin/gnome-keyring-daemon").exists()
|
||||
|| Path::new("/usr/bin/gnome-keyring").exists()
|
||||
|| Path::new("/usr/bin/kwalletd5").exists()
|
||||
|| Path::new("/usr/bin/kwalletd6").exists()
|
||||
|| env::var("SECRET_SERVICE").is_ok();
|
||||
|
||||
if has_dbus && has_keyring {
|
||||
KeyringStatus::Available
|
||||
} else {
|
||||
KeyringStatus::Unavailable
|
||||
}
|
||||
}
|
||||
|
||||
pub fn status(&self) -> KeyringStatus {
|
||||
self.status
|
||||
}
|
||||
|
||||
pub fn is_available(&self) -> bool {
|
||||
self.status == KeyringStatus::Available
|
||||
}
|
||||
|
||||
pub fn store_api_key(&self, provider: &str, api_key: &str) -> Result<()> {
|
||||
if !self.is_available() {
|
||||
bail!("Keyring is not available on this system");
|
||||
}
|
||||
|
||||
let entry = keyring::Entry::new(SERVICE_NAME, provider)
|
||||
.context("Failed to create keyring entry")?;
|
||||
|
||||
entry.set_password(api_key)
|
||||
.context("Failed to store API key")?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_api_key(&self, provider: &str) -> Result<Option<String>> {
|
||||
if let Ok(key) = env::var(ENV_API_KEY) {
|
||||
if !key.is_empty() {
|
||||
return Ok(Some(key));
|
||||
}
|
||||
}
|
||||
|
||||
if !self.is_available() {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let entry = keyring::Entry::new(SERVICE_NAME, provider)
|
||||
.context("Failed to create keyring entry")?;
|
||||
|
||||
match entry.get_password() {
|
||||
Ok(key) => Ok(Some(key)),
|
||||
Err(keyring::Error::NoEntry) => Ok(None),
|
||||
Err(e) => Err(e.into()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn delete_api_key(&self, provider: &str) -> Result<()> {
|
||||
if !self.is_available() {
|
||||
bail!("Keyring is not available on this system");
|
||||
}
|
||||
|
||||
let entry = keyring::Entry::new(SERVICE_NAME, provider)
|
||||
.context("Failed to create keyring entry")?;
|
||||
|
||||
entry.delete_credential()
|
||||
.context("Failed to delete API key")?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn has_api_key(&self, provider: &str) -> bool {
|
||||
self.get_api_key(provider).unwrap_or(None).is_some()
|
||||
}
|
||||
|
||||
fn make_pat_service_name(profile_name: &str) -> String {
|
||||
format!("{}/{}", PAT_SERVICE_PREFIX, profile_name)
|
||||
}
|
||||
|
||||
pub fn store_pat(&self, profile_name: &str, user_email: &str, service: &str, token: &str) -> Result<()> {
|
||||
if !self.is_available() {
|
||||
bail!("Keyring is not available on this system");
|
||||
}
|
||||
|
||||
let keyring_service = Self::make_pat_service_name(profile_name);
|
||||
let keyring_user = format!("{}:{}", user_email, service);
|
||||
|
||||
let entry = keyring::Entry::new(&keyring_service, &keyring_user)
|
||||
.context("Failed to create keyring entry for PAT")?;
|
||||
|
||||
entry.set_password(token)
|
||||
.context("Failed to store PAT in keyring")?;
|
||||
|
||||
eprintln!("[DEBUG] PAT stored in keyring: service={}, user={}", keyring_service, keyring_user);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_pat(&self, profile_name: &str, user_email: &str, service: &str) -> Result<Option<String>> {
|
||||
if !self.is_available() {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let keyring_service = Self::make_pat_service_name(profile_name);
|
||||
let keyring_user = format!("{}:{}", user_email, service);
|
||||
|
||||
let entry = keyring::Entry::new(&keyring_service, &keyring_user)
|
||||
.context("Failed to create keyring entry for PAT")?;
|
||||
|
||||
match entry.get_password() {
|
||||
Ok(token) => {
|
||||
eprintln!("[DEBUG] PAT retrieved from keyring: service={}, user={}", keyring_service, keyring_user);
|
||||
Ok(Some(token))
|
||||
}
|
||||
Err(keyring::Error::NoEntry) => {
|
||||
eprintln!("[DEBUG] PAT not found in keyring: service={}, user={}", keyring_service, keyring_user);
|
||||
Ok(None)
|
||||
}
|
||||
Err(e) => Err(e.into()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn delete_pat(&self, profile_name: &str, user_email: &str, service: &str) -> Result<()> {
|
||||
if !self.is_available() {
|
||||
bail!("Keyring is not available on this system");
|
||||
}
|
||||
|
||||
let keyring_service = Self::make_pat_service_name(profile_name);
|
||||
let keyring_user = format!("{}:{}", user_email, service);
|
||||
|
||||
let entry = keyring::Entry::new(&keyring_service, &keyring_user)
|
||||
.context("Failed to create keyring entry for PAT")?;
|
||||
|
||||
entry.delete_credential()
|
||||
.context("Failed to delete PAT from keyring")?;
|
||||
|
||||
eprintln!("[DEBUG] PAT deleted from keyring: service={}, user={}", keyring_service, keyring_user);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn has_pat(&self, profile_name: &str, user_email: &str, service: &str) -> bool {
|
||||
self.get_pat(profile_name, user_email, service).unwrap_or(None).is_some()
|
||||
}
|
||||
|
||||
pub fn delete_all_pats_for_profile(&self, profile_name: &str, user_email: &str, services: &[String]) -> Result<()> {
|
||||
for service in services {
|
||||
if let Err(e) = self.delete_pat(profile_name, user_email, service) {
|
||||
eprintln!("[DEBUG] Failed to delete PAT for service '{}': {}", service, e);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_status_message(&self) -> String {
|
||||
match self.status {
|
||||
KeyringStatus::Available => {
|
||||
#[cfg(target_os = "windows")]
|
||||
{
|
||||
"Windows Credential Manager is available".to_string()
|
||||
}
|
||||
#[cfg(target_os = "macos")]
|
||||
{
|
||||
"macOS Keychain is available".to_string()
|
||||
}
|
||||
#[cfg(target_os = "linux")]
|
||||
{
|
||||
"Linux secret service is available".to_string()
|
||||
}
|
||||
#[cfg(not(any(target_os = "windows", target_os = "macos", target_os = "linux")))]
|
||||
{
|
||||
"Keyring is available".to_string()
|
||||
}
|
||||
}
|
||||
KeyringStatus::Unavailable => {
|
||||
"Keyring is not available. Set QUICOMMIT_API_KEY environment variable.".to_string()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for KeyringManager {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_default_base_url(provider: &str) -> &'static str {
|
||||
match provider {
|
||||
"openai" => "https://api.openai.com/v1",
|
||||
"anthropic" => "https://api.anthropic.com/v1",
|
||||
"kimi" => "https://api.moonshot.cn/v1",
|
||||
"deepseek" => "https://api.deepseek.com/v1",
|
||||
"openrouter" => "https://openrouter.ai/api/v1",
|
||||
"ollama" => "http://localhost:11434",
|
||||
_ => "",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_default_model(provider: &str) -> &'static str {
|
||||
match provider {
|
||||
"openai" => "gpt-4",
|
||||
"anthropic" => "claude-3-sonnet-20240229",
|
||||
"kimi" => "moonshot-v1-8k",
|
||||
"deepseek" => "deepseek-chat",
|
||||
"openrouter" => "openai/gpt-3.5-turbo",
|
||||
"ollama" => "llama2",
|
||||
_ => "",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_supported_providers() -> &'static [&'static str] {
|
||||
&["ollama", "openai", "anthropic", "kimi", "deepseek", "openrouter"]
|
||||
}
|
||||
|
||||
pub fn provider_needs_api_key(provider: &str) -> bool {
|
||||
provider != "ollama"
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_get_default_base_url() {
|
||||
assert_eq!(get_default_base_url("openai"), "https://api.openai.com/v1");
|
||||
assert_eq!(get_default_base_url("anthropic"), "https://api.anthropic.com/v1");
|
||||
assert_eq!(get_default_base_url("kimi"), "https://api.moonshot.cn/v1");
|
||||
assert_eq!(get_default_base_url("deepseek"), "https://api.deepseek.com/v1");
|
||||
assert_eq!(get_default_base_url("openrouter"), "https://openrouter.ai/api/v1");
|
||||
assert_eq!(get_default_base_url("ollama"), "http://localhost:11434");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_default_model() {
|
||||
assert_eq!(get_default_model("openai"), "gpt-4");
|
||||
assert_eq!(get_default_model("anthropic"), "claude-3-sonnet-20240229");
|
||||
assert_eq!(get_default_model("ollama"), "llama2");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_provider_needs_api_key() {
|
||||
assert!(provider_needs_api_key("openai"));
|
||||
assert!(provider_needs_api_key("anthropic"));
|
||||
assert!(!provider_needs_api_key("ollama"));
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
pub mod crypto;
|
||||
pub mod editor;
|
||||
pub mod formatter;
|
||||
pub mod keyring;
|
||||
pub mod validators;
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
|
||||
@@ -58,11 +58,6 @@ lazy_static! {
|
||||
r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"
|
||||
).unwrap();
|
||||
|
||||
/// Regex for SSH key validation (basic)
|
||||
static ref SSH_KEY_REGEX: Regex = Regex::new(
|
||||
r"^(ssh-rsa|ssh-ed25519|ecdsa-sha2-nistp256|ecdsa-sha2-nistp384|ecdsa-sha2-nistp521)\s+[A-Za-z0-9+/]+={0,2}\s+.*$"
|
||||
).unwrap();
|
||||
|
||||
/// Regex for GPG key ID validation
|
||||
static ref GPG_KEY_ID_REGEX: Regex = Regex::new(
|
||||
r"^[A-F0-9]{16,40}$"
|
||||
@@ -81,7 +76,6 @@ pub fn validate_conventional_commit(message: &str) -> Result<()> {
|
||||
);
|
||||
}
|
||||
|
||||
// Check description length (max 100 chars for first line)
|
||||
if first_line.len() > 100 {
|
||||
bail!("Commit subject too long (max 100 characters)");
|
||||
}
|
||||
@@ -93,7 +87,6 @@ pub fn validate_conventional_commit(message: &str) -> Result<()> {
|
||||
pub fn validate_commitlint_commit(message: &str) -> Result<()> {
|
||||
let first_line = message.lines().next().unwrap_or("");
|
||||
|
||||
// Commitlint is more lenient but still requires type prefix
|
||||
let parts: Vec<&str> = first_line.splitn(2, ':').collect();
|
||||
if parts.len() != 2 {
|
||||
bail!("Invalid commit format. Expected: <type>[optional scope]: <subject>");
|
||||
@@ -102,7 +95,6 @@ pub fn validate_commitlint_commit(message: &str) -> Result<()> {
|
||||
let type_part = parts[0];
|
||||
let subject = parts[1].trim();
|
||||
|
||||
// Extract type (handle scope and breaking indicator)
|
||||
let commit_type = type_part
|
||||
.split('(')
|
||||
.next()
|
||||
@@ -117,7 +109,6 @@ pub fn validate_commitlint_commit(message: &str) -> Result<()> {
|
||||
);
|
||||
}
|
||||
|
||||
// Validate subject
|
||||
if subject.is_empty() {
|
||||
bail!("Commit subject cannot be empty");
|
||||
}
|
||||
@@ -130,12 +121,10 @@ pub fn validate_commitlint_commit(message: &str) -> Result<()> {
|
||||
bail!("Commit subject too long (max 100 characters)");
|
||||
}
|
||||
|
||||
// Subject should not start with uppercase
|
||||
if subject.chars().next().map(|c| c.is_uppercase()).unwrap_or(false) {
|
||||
bail!("Commit subject should not start with uppercase letter");
|
||||
}
|
||||
|
||||
// Subject should not end with period
|
||||
if subject.ends_with('.') {
|
||||
bail!("Commit subject should not end with a period");
|
||||
}
|
||||
@@ -179,15 +168,6 @@ pub fn validate_email(email: &str) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Validate SSH key format
|
||||
pub fn validate_ssh_key(key: &str) -> Result<()> {
|
||||
if !SSH_KEY_REGEX.is_match(key.trim()) {
|
||||
bail!("Invalid SSH public key format");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Validate GPG key ID
|
||||
pub fn validate_gpg_key_id(key_id: &str) -> Result<()> {
|
||||
if !GPG_KEY_ID_REGEX.is_match(key_id) {
|
||||
|
||||
359
tests/config_export_import_tests.rs
Normal file
359
tests/config_export_import_tests.rs
Normal file
@@ -0,0 +1,359 @@
|
||||
use assert_cmd::Command;
|
||||
use predicates::prelude::*;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use tempfile::TempDir;
|
||||
|
||||
fn create_git_repo(dir: &PathBuf) -> std::process::Output {
|
||||
std::process::Command::new("git")
|
||||
.args(&["init"])
|
||||
.current_dir(dir)
|
||||
.output()
|
||||
.expect("Failed to init git repo")
|
||||
}
|
||||
|
||||
fn configure_git_user(dir: &PathBuf) {
|
||||
std::process::Command::new("git")
|
||||
.args(&["config", "user.name", "Test User"])
|
||||
.current_dir(dir)
|
||||
.output()
|
||||
.expect("Failed to configure git user name");
|
||||
|
||||
std::process::Command::new("git")
|
||||
.args(&["config", "user.email", "test@example.com"])
|
||||
.current_dir(dir)
|
||||
.output()
|
||||
.expect("Failed to configure git user email");
|
||||
}
|
||||
|
||||
fn setup_git_repo(dir: &PathBuf) {
|
||||
create_git_repo(dir);
|
||||
configure_git_user(dir);
|
||||
}
|
||||
|
||||
fn init_quicommit(config_path: &PathBuf) {
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["init", "--yes", "--config", config_path.to_str().unwrap()]);
|
||||
cmd.assert().success();
|
||||
}
|
||||
|
||||
mod config_export {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_export_to_stdout() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let config_path = temp_dir.path().join("config.toml");
|
||||
init_quicommit(&config_path);
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["config", "export", "--config", config_path.to_str().unwrap()]);
|
||||
|
||||
cmd.assert()
|
||||
.success()
|
||||
.stdout(predicate::str::contains("version"))
|
||||
.stdout(predicate::str::contains("[llm]"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_export_to_file() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let config_path = temp_dir.path().join("config.toml");
|
||||
let export_path = temp_dir.path().join("exported.toml");
|
||||
init_quicommit(&config_path);
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&[
|
||||
"config", "export",
|
||||
"--config", config_path.to_str().unwrap(),
|
||||
"--output", export_path.to_str().unwrap(),
|
||||
"--password", ""
|
||||
]);
|
||||
|
||||
cmd.assert()
|
||||
.success()
|
||||
.stdout(predicate::str::contains("Configuration exported"));
|
||||
|
||||
assert!(export_path.exists(), "Export file should be created");
|
||||
|
||||
let content = fs::read_to_string(&export_path).unwrap();
|
||||
assert!(content.contains("version"), "Export should contain version");
|
||||
assert!(content.contains("[llm]"), "Export should contain LLM config");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_export_encrypted() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let config_path = temp_dir.path().join("config.toml");
|
||||
let export_path = temp_dir.path().join("encrypted.toml");
|
||||
init_quicommit(&config_path);
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&[
|
||||
"config", "export",
|
||||
"--config", config_path.to_str().unwrap(),
|
||||
"--output", export_path.to_str().unwrap(),
|
||||
"--password", "test_password_123"
|
||||
]);
|
||||
|
||||
cmd.assert()
|
||||
.success()
|
||||
.stdout(predicate::str::contains("encrypted and exported"));
|
||||
|
||||
assert!(export_path.exists(), "Export file should be created");
|
||||
|
||||
let content = fs::read_to_string(&export_path).unwrap();
|
||||
assert!(content.starts_with("ENCRYPTED:"), "Encrypted file should start with ENCRYPTED:");
|
||||
assert!(!content.contains("[llm]"), "Encrypted content should not be readable");
|
||||
}
|
||||
}
|
||||
|
||||
mod config_import {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_import_plain_config() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let config_path = temp_dir.path().join("config.toml");
|
||||
let import_path = temp_dir.path().join("import.toml");
|
||||
|
||||
let plain_config = r#"
|
||||
version = "1"
|
||||
|
||||
[llm]
|
||||
provider = "openai"
|
||||
model = "gpt-4"
|
||||
max_tokens = 1000
|
||||
temperature = 0.7
|
||||
timeout = 60
|
||||
api_key_storage = "keyring"
|
||||
|
||||
[commit]
|
||||
format = "conventional"
|
||||
auto_generate = true
|
||||
allow_empty = false
|
||||
gpg_sign = false
|
||||
max_subject_length = 100
|
||||
require_scope = false
|
||||
require_body = false
|
||||
body_required_types = ["feat", "fix"]
|
||||
|
||||
[tag]
|
||||
version_prefix = "v"
|
||||
auto_generate = true
|
||||
gpg_sign = false
|
||||
include_changelog = true
|
||||
|
||||
[changelog]
|
||||
path = "CHANGELOG.md"
|
||||
auto_generate = true
|
||||
format = "keep-a-changelog"
|
||||
include_hashes = false
|
||||
include_authors = false
|
||||
group_by_type = true
|
||||
|
||||
[theme]
|
||||
colors = true
|
||||
icons = true
|
||||
date_format = "%Y-%m-%d"
|
||||
|
||||
[language]
|
||||
output_language = "en"
|
||||
keep_types_english = true
|
||||
keep_changelog_types_english = true
|
||||
"#;
|
||||
fs::write(&import_path, plain_config).unwrap();
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&[
|
||||
"config", "import",
|
||||
"--config", config_path.to_str().unwrap(),
|
||||
"--file", import_path.to_str().unwrap()
|
||||
]);
|
||||
|
||||
cmd.assert()
|
||||
.success()
|
||||
.stdout(predicate::str::contains("Configuration imported"));
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["config", "get", "llm.provider", "--config", config_path.to_str().unwrap()]);
|
||||
cmd.assert()
|
||||
.success()
|
||||
.stdout(predicate::str::contains("openai"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_import_encrypted_config() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let config_path1 = temp_dir.path().join("config1.toml");
|
||||
let config_path2 = temp_dir.path().join("config2.toml");
|
||||
let export_path = temp_dir.path().join("encrypted.toml");
|
||||
|
||||
init_quicommit(&config_path1);
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&[
|
||||
"config", "set", "llm.provider", "anthropic",
|
||||
"--config", config_path1.to_str().unwrap()
|
||||
]);
|
||||
cmd.assert().success();
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&[
|
||||
"config", "export",
|
||||
"--config", config_path1.to_str().unwrap(),
|
||||
"--output", export_path.to_str().unwrap(),
|
||||
"--password", "secure_password"
|
||||
]);
|
||||
cmd.assert().success();
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&[
|
||||
"config", "import",
|
||||
"--config", config_path2.to_str().unwrap(),
|
||||
"--file", export_path.to_str().unwrap(),
|
||||
"--password", "secure_password"
|
||||
]);
|
||||
cmd.assert()
|
||||
.success()
|
||||
.stdout(predicate::str::contains("Configuration imported"));
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["config", "get", "llm.provider", "--config", config_path2.to_str().unwrap()]);
|
||||
cmd.assert()
|
||||
.success()
|
||||
.stdout(predicate::str::contains("anthropic"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_import_encrypted_wrong_password() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let config_path = temp_dir.path().join("config.toml");
|
||||
let export_path = temp_dir.path().join("encrypted.toml");
|
||||
|
||||
init_quicommit(&config_path);
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&[
|
||||
"config", "export",
|
||||
"--config", config_path.to_str().unwrap(),
|
||||
"--output", export_path.to_str().unwrap(),
|
||||
"--password", "correct_password"
|
||||
]);
|
||||
cmd.assert().success();
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&[
|
||||
"config", "import",
|
||||
"--config", config_path.to_str().unwrap(),
|
||||
"--file", export_path.to_str().unwrap(),
|
||||
"--password", "wrong_password"
|
||||
]);
|
||||
cmd.assert()
|
||||
.failure()
|
||||
.stderr(predicate::str::contains("Failed to decrypt"));
|
||||
}
|
||||
}
|
||||
|
||||
mod config_export_import_roundtrip {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_roundtrip_plain() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let config_path1 = temp_dir.path().join("config1.toml");
|
||||
let config_path2 = temp_dir.path().join("config2.toml");
|
||||
let export_path = temp_dir.path().join("export.toml");
|
||||
|
||||
init_quicommit(&config_path1);
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&[
|
||||
"config", "set", "llm.model", "gpt-4-turbo",
|
||||
"--config", config_path1.to_str().unwrap()
|
||||
]);
|
||||
cmd.assert().success();
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&[
|
||||
"config", "export",
|
||||
"--config", config_path1.to_str().unwrap(),
|
||||
"--output", export_path.to_str().unwrap(),
|
||||
"--password", ""
|
||||
]);
|
||||
cmd.assert().success();
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&[
|
||||
"config", "import",
|
||||
"--config", config_path2.to_str().unwrap(),
|
||||
"--file", export_path.to_str().unwrap()
|
||||
]);
|
||||
cmd.assert().success();
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["config", "get", "llm.model", "--config", config_path2.to_str().unwrap()]);
|
||||
cmd.assert()
|
||||
.success()
|
||||
.stdout(predicate::str::contains("gpt-4-turbo"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_roundtrip_encrypted() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let config_path1 = temp_dir.path().join("config1.toml");
|
||||
let config_path2 = temp_dir.path().join("config2.toml");
|
||||
let export_path = temp_dir.path().join("encrypted.toml");
|
||||
let password = "my_secure_password_123";
|
||||
|
||||
init_quicommit(&config_path1);
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&[
|
||||
"config", "set", "llm.provider", "deepseek",
|
||||
"--config", config_path1.to_str().unwrap()
|
||||
]);
|
||||
cmd.assert().success();
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&[
|
||||
"config", "set", "llm.model", "deepseek-chat",
|
||||
"--config", config_path1.to_str().unwrap()
|
||||
]);
|
||||
cmd.assert().success();
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&[
|
||||
"config", "export",
|
||||
"--config", config_path1.to_str().unwrap(),
|
||||
"--output", export_path.to_str().unwrap(),
|
||||
"--password", password
|
||||
]);
|
||||
cmd.assert().success();
|
||||
|
||||
let exported_content = fs::read_to_string(&export_path).unwrap();
|
||||
assert!(exported_content.starts_with("ENCRYPTED:"));
|
||||
assert!(!exported_content.contains("deepseek"));
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&[
|
||||
"config", "import",
|
||||
"--config", config_path2.to_str().unwrap(),
|
||||
"--file", export_path.to_str().unwrap(),
|
||||
"--password", password
|
||||
]);
|
||||
cmd.assert().success();
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["config", "get", "llm.provider", "--config", config_path2.to_str().unwrap()]);
|
||||
cmd.assert()
|
||||
.success()
|
||||
.stdout(predicate::str::contains("deepseek"));
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["config", "get", "llm.model", "--config", config_path2.to_str().unwrap()]);
|
||||
cmd.assert()
|
||||
.success()
|
||||
.stdout(predicate::str::contains("deepseek-chat"));
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,84 @@
|
||||
use assert_cmd::Command;
|
||||
use predicates::prelude::*;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use tempfile::TempDir;
|
||||
|
||||
fn create_git_repo(dir: &PathBuf) -> std::process::Output {
|
||||
std::process::Command::new("git")
|
||||
.args(&["init"])
|
||||
.current_dir(dir)
|
||||
.output()
|
||||
.expect("Failed to init git repo")
|
||||
}
|
||||
|
||||
fn configure_git_user(dir: &PathBuf) {
|
||||
std::process::Command::new("git")
|
||||
.args(&["config", "user.name", "Test User"])
|
||||
.current_dir(dir)
|
||||
.output()
|
||||
.expect("Failed to configure git user name");
|
||||
|
||||
std::process::Command::new("git")
|
||||
.args(&["config", "user.email", "test@example.com"])
|
||||
.current_dir(dir)
|
||||
.output()
|
||||
.expect("Failed to configure git user email");
|
||||
}
|
||||
|
||||
fn create_test_file(dir: &PathBuf, name: &str, content: &str) {
|
||||
let file_path = dir.join(name);
|
||||
fs::write(&file_path, content).expect("Failed to create test file");
|
||||
}
|
||||
|
||||
fn stage_file(dir: &PathBuf, name: &str) {
|
||||
std::process::Command::new("git")
|
||||
.args(&["add", name])
|
||||
.current_dir(dir)
|
||||
.output()
|
||||
.expect("Failed to stage file");
|
||||
}
|
||||
|
||||
fn create_commit(dir: &PathBuf, message: &str) {
|
||||
std::process::Command::new("git")
|
||||
.args(&["commit", "-m", message])
|
||||
.current_dir(dir)
|
||||
.output()
|
||||
.expect("Failed to create commit");
|
||||
}
|
||||
|
||||
fn setup_git_repo(dir: &PathBuf) {
|
||||
create_git_repo(dir);
|
||||
configure_git_user(dir);
|
||||
}
|
||||
|
||||
fn setup_test_repo_with_file(dir: &PathBuf, file_name: &str, file_content: &str) {
|
||||
setup_git_repo(dir);
|
||||
create_test_file(dir, file_name, file_content);
|
||||
stage_file(dir, file_name);
|
||||
}
|
||||
|
||||
fn init_quicommit(dir: &PathBuf, config_path: &PathBuf) {
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["init", "--yes", "--config", config_path.to_str().unwrap()])
|
||||
.current_dir(dir);
|
||||
cmd.assert().success();
|
||||
}
|
||||
|
||||
mod cli_basic {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_cli_help() {
|
||||
fn test_help() {
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.arg("--help");
|
||||
cmd.assert()
|
||||
.success()
|
||||
.stdout(predicate::str::contains("QuiCommit"));
|
||||
.stdout(predicate::str::contains("QuiCommit"))
|
||||
.stdout(predicate::str::contains("AI-powered Git assistant"))
|
||||
.stdout(predicate::str::contains("Usage:"))
|
||||
.stdout(predicate::str::contains("Commands:"))
|
||||
.stdout(predicate::str::contains("Options:"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -18,47 +87,574 @@ fn test_version() {
|
||||
cmd.arg("--version");
|
||||
cmd.assert()
|
||||
.success()
|
||||
.stdout(predicate::str::contains("0.1.0"));
|
||||
.stdout(predicate::str::contains("quicommit"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_config_show() {
|
||||
fn test_no_args_shows_help() {
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.assert()
|
||||
.failure()
|
||||
.stderr(predicate::str::contains("Usage:"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_verbose_flag() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let config_dir = temp_dir.path().join("config");
|
||||
fs::create_dir(&config_dir).unwrap();
|
||||
let repo_path = temp_dir.path().to_path_buf();
|
||||
let config_path = repo_path.join("config.toml");
|
||||
create_git_repo(&repo_path);
|
||||
configure_git_user(&repo_path);
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.env("QUICOMMIT_CONFIG", config_dir.join("config.toml"))
|
||||
.arg("config")
|
||||
.arg("show");
|
||||
cmd.args(&["-vv", "init", "--yes", "--config", config_path.to_str().unwrap()])
|
||||
.current_dir(&repo_path);
|
||||
|
||||
cmd.assert().success();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_profile_list_empty() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.env("QUICOMMIT_CONFIG", temp_dir.path().join("config.toml"))
|
||||
.arg("profile")
|
||||
.arg("list");
|
||||
|
||||
cmd.assert()
|
||||
.success()
|
||||
.stdout(predicate::str::contains("No profiles configured"));
|
||||
}
|
||||
|
||||
mod init_command {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_init_quick() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let config_path = temp_dir.path().join("config.toml");
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.env("QUICOMMIT_CONFIG", temp_dir.path().join("config.toml"))
|
||||
.arg("init")
|
||||
.arg("--yes");
|
||||
cmd.args(&["init", "--yes", "--config", config_path.to_str().unwrap()]);
|
||||
|
||||
cmd.assert()
|
||||
.success()
|
||||
.stdout(predicate::str::contains("initialized successfully"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_init_creates_config_file() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let config_path = temp_dir.path().join("config.toml");
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["init", "--yes", "--config", config_path.to_str().unwrap()]);
|
||||
|
||||
cmd.assert().success();
|
||||
|
||||
assert!(config_path.exists(), "Config file should be created");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_init_in_git_repo() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let repo_path = temp_dir.path().to_path_buf();
|
||||
create_git_repo(&repo_path);
|
||||
configure_git_user(&repo_path);
|
||||
|
||||
let config_path = repo_path.join("test_config.toml");
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["init", "--yes", "--config", config_path.to_str().unwrap()])
|
||||
.current_dir(&repo_path);
|
||||
|
||||
cmd.assert().success();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_init_reset() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let config_path = temp_dir.path().join("config.toml");
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["init", "--yes", "--config", config_path.to_str().unwrap()]);
|
||||
cmd.assert().success();
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["init", "--yes", "--reset", "--config", config_path.to_str().unwrap()]);
|
||||
cmd.assert()
|
||||
.success()
|
||||
.stdout(predicate::str::contains("initialized successfully"));
|
||||
}
|
||||
}
|
||||
|
||||
mod profile_command {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_profile_list_empty() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let config_path = temp_dir.path().join("config.toml");
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["profile", "list", "--config", config_path.to_str().unwrap()]);
|
||||
|
||||
cmd.assert()
|
||||
.success()
|
||||
.stdout(predicate::str::contains("No profiles"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_profile_list_with_profile() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let config_path = temp_dir.path().join("config.toml");
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["init", "--yes", "--config", config_path.to_str().unwrap()]);
|
||||
cmd.assert().success();
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["profile", "list", "--config", config_path.to_str().unwrap()]);
|
||||
|
||||
cmd.assert()
|
||||
.success()
|
||||
.stdout(predicate::str::contains("default"));
|
||||
}
|
||||
}
|
||||
|
||||
mod config_command {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_config_show() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let config_path = temp_dir.path().join("config.toml");
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["init", "--yes", "--config", config_path.to_str().unwrap()]);
|
||||
cmd.assert().success();
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["config", "show", "--config", config_path.to_str().unwrap()]);
|
||||
|
||||
cmd.assert()
|
||||
.success()
|
||||
.stdout(predicate::str::contains("Configuration"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_config_path() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let config_path = temp_dir.path().join("config.toml");
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["init", "--yes", "--config", config_path.to_str().unwrap()]);
|
||||
cmd.assert().success();
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["config", "path", "--config", config_path.to_str().unwrap()]);
|
||||
|
||||
cmd.assert()
|
||||
.success()
|
||||
.stdout(predicate::str::contains("config.toml"));
|
||||
}
|
||||
}
|
||||
|
||||
mod commit_command {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_commit_no_repo() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let config_path = temp_dir.path().join("config.toml");
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["init", "--yes", "--config", config_path.to_str().unwrap()]);
|
||||
cmd.assert().success();
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["commit", "--dry-run", "--yes", "--config", config_path.to_str().unwrap()])
|
||||
.current_dir(temp_dir.path());
|
||||
|
||||
cmd.assert()
|
||||
.failure()
|
||||
.stderr(predicate::str::contains("git").or(predicate::str::contains("repository")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_commit_no_changes() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let repo_path = temp_dir.path().to_path_buf();
|
||||
setup_git_repo(&repo_path);
|
||||
|
||||
let config_path = repo_path.join("config.toml");
|
||||
init_quicommit(&repo_path, &config_path);
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["commit", "--manual", "-m", "test: empty", "--dry-run", "--yes", "--config", config_path.to_str().unwrap()])
|
||||
.current_dir(&repo_path);
|
||||
|
||||
cmd.assert()
|
||||
.success()
|
||||
.stdout(predicate::str::contains("Dry run"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_commit_with_staged_changes() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let repo_path = temp_dir.path().to_path_buf();
|
||||
setup_test_repo_with_file(&repo_path, "test.txt", "Hello, World!");
|
||||
|
||||
let config_path = repo_path.join("config.toml");
|
||||
init_quicommit(&repo_path, &config_path);
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["commit", "--manual", "-m", "test: add test file", "--dry-run", "--yes", "--config", config_path.to_str().unwrap()])
|
||||
.current_dir(&repo_path);
|
||||
|
||||
cmd.assert()
|
||||
.success()
|
||||
.stdout(predicate::str::contains("Dry run"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_commit_date_mode() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let repo_path = temp_dir.path().to_path_buf();
|
||||
setup_test_repo_with_file(&repo_path, "daily.txt", "Daily update");
|
||||
|
||||
let config_path = repo_path.join("config.toml");
|
||||
init_quicommit(&repo_path, &config_path);
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["commit", "--date", "--dry-run", "--yes", "--config", config_path.to_str().unwrap()])
|
||||
.current_dir(&repo_path);
|
||||
|
||||
cmd.assert()
|
||||
.success()
|
||||
.stdout(predicate::str::contains("Dry run"));
|
||||
}
|
||||
}
|
||||
|
||||
mod tag_command {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_tag_no_repo() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let config_path = temp_dir.path().join("config.toml");
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["init", "--yes", "--config", config_path.to_str().unwrap()]);
|
||||
cmd.assert().success();
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["tag", "--dry-run", "--yes", "--config", config_path.to_str().unwrap()])
|
||||
.current_dir(temp_dir.path());
|
||||
|
||||
cmd.assert()
|
||||
.failure()
|
||||
.stderr(predicate::str::contains("git").or(predicate::str::contains("repository")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tag_list_empty() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let repo_path = temp_dir.path().to_path_buf();
|
||||
setup_git_repo(&repo_path);
|
||||
|
||||
create_test_file(&repo_path, "test.txt", "content");
|
||||
stage_file(&repo_path, "test.txt");
|
||||
create_commit(&repo_path, "feat: initial commit");
|
||||
|
||||
let config_path = repo_path.join("config.toml");
|
||||
init_quicommit(&repo_path, &config_path);
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["tag", "--name", "v0.1.0", "--dry-run", "--yes", "--config", config_path.to_str().unwrap()])
|
||||
.current_dir(&repo_path);
|
||||
|
||||
cmd.assert()
|
||||
.success()
|
||||
.stdout(predicate::str::contains("v0.1.0"));
|
||||
}
|
||||
}
|
||||
|
||||
mod changelog_command {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_changelog_init() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let repo_path = temp_dir.path().to_path_buf();
|
||||
setup_git_repo(&repo_path);
|
||||
|
||||
let config_path = repo_path.join("config.toml");
|
||||
let changelog_path = repo_path.join("CHANGELOG.md");
|
||||
|
||||
init_quicommit(&repo_path, &config_path);
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["changelog", "--init", "--output", changelog_path.to_str().unwrap(), "--config", config_path.to_str().unwrap()])
|
||||
.current_dir(&repo_path);
|
||||
|
||||
cmd.assert().success();
|
||||
|
||||
assert!(changelog_path.exists(), "Changelog file should be created");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_changelog_dry_run() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let repo_path = temp_dir.path().to_path_buf();
|
||||
setup_git_repo(&repo_path);
|
||||
|
||||
create_test_file(&repo_path, "test.txt", "content");
|
||||
stage_file(&repo_path, "test.txt");
|
||||
create_commit(&repo_path, "feat: add feature");
|
||||
|
||||
let config_path = repo_path.join("config.toml");
|
||||
init_quicommit(&repo_path, &config_path);
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["changelog", "--dry-run", "--yes", "--config", config_path.to_str().unwrap()])
|
||||
.current_dir(&repo_path);
|
||||
|
||||
cmd.assert()
|
||||
.success();
|
||||
}
|
||||
}
|
||||
|
||||
mod cross_platform {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_path_handling_windows_style() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let config_path = temp_dir.path().join("subdir").join("config.toml");
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["init", "--yes", "--config", config_path.to_str().unwrap()]);
|
||||
|
||||
cmd.assert().success();
|
||||
assert!(config_path.exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_config_with_spaces_in_path() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let space_dir = temp_dir.path().join("path with spaces");
|
||||
fs::create_dir_all(&space_dir).unwrap();
|
||||
let config_path = space_dir.join("config.toml");
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["init", "--yes", "--config", config_path.to_str().unwrap()]);
|
||||
|
||||
cmd.assert().success();
|
||||
assert!(config_path.exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_config_with_unicode_path() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let unicode_dir = temp_dir.path().join("路径测试");
|
||||
fs::create_dir_all(&unicode_dir).unwrap();
|
||||
let config_path = unicode_dir.join("config.toml");
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["init", "--yes", "--config", config_path.to_str().unwrap()]);
|
||||
|
||||
cmd.assert().success();
|
||||
assert!(config_path.exists());
|
||||
}
|
||||
}
|
||||
|
||||
mod git_operations {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_git_repo_detection() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let repo_path = temp_dir.path().to_path_buf();
|
||||
create_git_repo(&repo_path);
|
||||
configure_git_user(&repo_path);
|
||||
|
||||
let git_dir = repo_path.join(".git");
|
||||
assert!(git_dir.exists(), ".git directory should exist");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_git_status_clean() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let repo_path = temp_dir.path().to_path_buf();
|
||||
create_git_repo(&repo_path);
|
||||
configure_git_user(&repo_path);
|
||||
|
||||
let output = std::process::Command::new("git")
|
||||
.args(&["status", "--porcelain"])
|
||||
.current_dir(&repo_path)
|
||||
.output()
|
||||
.expect("Failed to run git status");
|
||||
|
||||
assert!(output.status.success());
|
||||
assert!(String::from_utf8_lossy(&output.stdout).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_git_commit_creation() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let repo_path = temp_dir.path().to_path_buf();
|
||||
create_git_repo(&repo_path);
|
||||
configure_git_user(&repo_path);
|
||||
|
||||
create_test_file(&repo_path, "test.txt", "content");
|
||||
stage_file(&repo_path, "test.txt");
|
||||
create_commit(&repo_path, "feat: initial commit");
|
||||
|
||||
let output = std::process::Command::new("git")
|
||||
.args(&["log", "--oneline"])
|
||||
.current_dir(&repo_path)
|
||||
.output()
|
||||
.expect("Failed to run git log");
|
||||
|
||||
assert!(output.status.success());
|
||||
let log = String::from_utf8_lossy(&output.stdout);
|
||||
assert!(log.contains("initial commit"));
|
||||
}
|
||||
}
|
||||
|
||||
mod validators {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_commit_message_validation() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let repo_path = temp_dir.path().to_path_buf();
|
||||
setup_test_repo_with_file(&repo_path, "test.txt", "content");
|
||||
|
||||
let config_path = repo_path.join("config.toml");
|
||||
init_quicommit(&repo_path, &config_path);
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["commit", "--manual", "-m", "invalid commit message without type", "--dry-run", "--yes", "--config", config_path.to_str().unwrap()])
|
||||
.current_dir(&repo_path);
|
||||
|
||||
cmd.assert()
|
||||
.failure()
|
||||
.stderr(predicate::str::contains("Invalid").or(predicate::str::contains("format")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_valid_conventional_commit() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let repo_path = temp_dir.path().to_path_buf();
|
||||
setup_test_repo_with_file(&repo_path, "test.txt", "content");
|
||||
|
||||
let config_path = repo_path.join("config.toml");
|
||||
init_quicommit(&repo_path, &config_path);
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["commit", "--manual", "-m", "feat: add new feature", "--dry-run", "--yes", "--config", config_path.to_str().unwrap()])
|
||||
.current_dir(&repo_path);
|
||||
|
||||
cmd.assert()
|
||||
.success()
|
||||
.stdout(predicate::str::contains("Dry run"));
|
||||
}
|
||||
}
|
||||
|
||||
mod subcommands {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_commit_alias() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let repo_path = temp_dir.path().to_path_buf();
|
||||
setup_test_repo_with_file(&repo_path, "test.txt", "content");
|
||||
|
||||
let config_path = repo_path.join("config.toml");
|
||||
init_quicommit(&repo_path, &config_path);
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["c", "--manual", "-m", "fix: test", "--dry-run", "--yes", "--config", config_path.to_str().unwrap()])
|
||||
.current_dir(&repo_path);
|
||||
|
||||
cmd.assert()
|
||||
.success()
|
||||
.stdout(predicate::str::contains("Dry run"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_init_alias() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let config_path = temp_dir.path().join("config.toml");
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["i", "--yes", "--config", config_path.to_str().unwrap()]);
|
||||
|
||||
cmd.assert()
|
||||
.success()
|
||||
.stdout(predicate::str::contains("initialized successfully"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_profile_alias() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let config_path = temp_dir.path().join("config.toml");
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["init", "--yes", "--config", config_path.to_str().unwrap()]);
|
||||
cmd.assert().success();
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["p", "list", "--config", config_path.to_str().unwrap()]);
|
||||
|
||||
cmd.assert()
|
||||
.success()
|
||||
.stdout(predicate::str::contains("default"));
|
||||
}
|
||||
}
|
||||
|
||||
mod edge_cases {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_config_file_not_found() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let non_existent_config = temp_dir.path().join("non_existent_config.toml");
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["config", "show", "--config", non_existent_config.to_str().unwrap()]);
|
||||
|
||||
cmd.assert()
|
||||
.success()
|
||||
.stdout(predicate::str::contains("QuiCommit Configuration"))
|
||||
.stdout(predicate::str::contains("Default profile: (none)"))
|
||||
.stdout(predicate::str::contains("Profiles: 0"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_invalid_git_repo() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let repo_path = temp_dir.path().to_path_buf();
|
||||
|
||||
let config_path = repo_path.join("config.toml");
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["init", "--yes", "--config", config_path.to_str().unwrap()]);
|
||||
cmd.assert().success();
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["commit", "--dry-run", "--yes", "--config", config_path.to_str().unwrap()])
|
||||
.current_dir(&repo_path);
|
||||
|
||||
cmd.assert()
|
||||
.failure()
|
||||
.stderr(predicate::str::contains("git").or(predicate::str::contains("repository")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_empty_commit_message() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let repo_path = temp_dir.path().to_path_buf();
|
||||
setup_test_repo_with_file(&repo_path, "test.txt", "content");
|
||||
|
||||
let config_path = repo_path.join("config.toml");
|
||||
init_quicommit(&repo_path, &config_path);
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["commit", "--manual", "-m", "", "--dry-run", "--yes", "--config", config_path.to_str().unwrap()])
|
||||
.current_dir(&repo_path);
|
||||
|
||||
cmd.assert()
|
||||
.failure()
|
||||
.stderr(predicate::str::contains("Invalid conventional commit format"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user