From 1cbb01ccc4458cac00648ff20433cf8eb817c7af Mon Sep 17 00:00:00 2001 From: SidneyZhang Date: Fri, 30 Jan 2026 18:29:11 +0800 Subject: [PATCH] In Git2 error fixing, opening the repo is still currently unavailable. --- CHANGELOG.md | 2 +- Cargo.toml | 2 +- README.md | 2 ++ readme_zh.md | 2 +- src/commands/changelog.rs | 2 +- src/commands/commit.rs | 2 +- src/commands/config.rs | 15 +++++++++++---- src/commands/profile.rs | 4 ++-- src/commands/tag.rs | 2 +- src/git/mod.rs | 28 +++++++++++++++++++++------- 10 files changed, 42 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b5e1dc7..7a1ed2f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added -- Initial release of QuicCommit +- Initial release of QuiCommit - AI-powered commit message generation using LLM APIs (OpenAI, Anthropic) or local Ollama - Support for Conventional Commits and @commitlint formats - Multiple Git profile management with SSH and GPG support diff --git a/Cargo.toml b/Cargo.toml index 468155c..1ff23b3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,7 +29,7 @@ toml = "0.8" dirs = "5.0" # Git operations -git2 = "0.18" +git2 = "0.20.3" which = "6.0" # HTTP client for LLM APIs diff --git a/README.md b/README.md index 0dbc685..1c8c373 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # QuiCommit +English | [中文文档](README_zh.md) + A powerful AI-powered Git assistant for generating conventional commits, tags, and changelogs. Manage multiple Git profiles for different work contexts. ![Rust](https://img.shields.io/badge/rust-%23000000.svg?style=for-the-badge&logo=rust&logoColor=white) diff --git a/readme_zh.md b/readme_zh.md index 6965e45..87988e9 100644 --- a/readme_zh.md +++ b/readme_zh.md @@ -1,6 +1,6 @@ # QuiCommit - +[English](README.md) | 简体中文 一款强大的AI驱动的Git助手,用于生成规范化的提交信息、标签和变更日志,并支持管理多个Git配置。 diff --git a/src/commands/changelog.rs b/src/commands/changelog.rs index 645c9eb..85a8a71 100644 --- a/src/commands/changelog.rs +++ b/src/commands/changelog.rs @@ -64,7 +64,7 @@ pub struct ChangelogCommand { impl ChangelogCommand { pub async fn execute(&self) -> Result<()> { - let repo = find_repo(".")?; + let repo = find_repo(std::env::current_dir()?.as_path())?; let manager = ConfigManager::new()?; let config = manager.config(); diff --git a/src/commands/commit.rs b/src/commands/commit.rs index bd26b5c..a55b863 100644 --- a/src/commands/commit.rs +++ b/src/commands/commit.rs @@ -77,7 +77,7 @@ pub struct CommitCommand { impl CommitCommand { pub async fn execute(&self) -> Result<()> { // Find git repository - let repo = find_repo(".")?; + let repo = find_repo(std::env::current_dir()?.as_path())?; // Check for changes let status = repo.status_summary()?; diff --git a/src/commands/config.rs b/src/commands/config.rs index c051294..7219e5c 100644 --- a/src/commands/config.rs +++ b/src/commands/config.rs @@ -1,10 +1,10 @@ -use anyhow::{bail, Context, Result}; +use anyhow::{bail, Result}; use clap::{Parser, Subcommand}; use colored::Colorize; use dialoguer::{Confirm, Input, Select}; use crate::config::manager::ConfigManager; -use crate::config::{CommitFormat, LlmConfig}; +use crate::config::CommitFormat; /// Mask API key with asterisks for security fn mask_api_key(key: Option<&str>) -> String { @@ -821,9 +821,16 @@ impl ConfigCommand { if client.is_available().await { println!("{} LLM connection successful!", "✓".green()); - // Test generation + // Test generation with a sample diff println!("Testing generation..."); - match client.generate_commit_message("test", crate::config::CommitFormat::Conventional).await { + let sample_diff = r#"diff --git a/src/main.rs b/src/main.rs +--- a/src/main.rs ++++ b/src/main.rs +@@ -1,3 +1,4 @@ + fn main() { ++ println!("Hello, World!"); + }"#; + match client.generate_commit_message(sample_diff, crate::config::CommitFormat::Conventional).await { Ok(response) => { println!("{} Generation test passed", "✓".green()); println!("Response: {}", response.description.dimmed()); diff --git a/src/commands/profile.rs b/src/commands/profile.rs index c935fbb..012b4c8 100644 --- a/src/commands/profile.rs +++ b/src/commands/profile.rs @@ -352,7 +352,7 @@ impl ProfileCommand { async fn set_repo(&self, name: &str) -> Result<()> { let mut manager = ConfigManager::new()?; - let repo = find_repo(".")?; + let repo = find_repo(std::env::current_dir()?.as_path())?; let repo_path = repo.path().to_string_lossy().to_string(); @@ -381,7 +381,7 @@ impl ProfileCommand { profile.apply_global()?; println!("{} Applied profile '{}' globally", "✓".green(), profile.name.cyan()); } else { - let repo = find_repo(".")?; + let repo = find_repo(std::env::current_dir()?.as_path())?; profile.apply_to_repo(repo.inner())?; println!("{} Applied profile '{}' to current repository", "✓".green(), profile.name.cyan()); } diff --git a/src/commands/tag.rs b/src/commands/tag.rs index ecdd62b..e7e51e1 100644 --- a/src/commands/tag.rs +++ b/src/commands/tag.rs @@ -61,7 +61,7 @@ pub struct TagCommand { impl TagCommand { pub async fn execute(&self) -> Result<()> { - let repo = find_repo(".")?; + let repo = find_repo(std::env::current_dir()?.as_path())?; let manager = ConfigManager::new()?; let config = manager.config(); diff --git a/src/git/mod.rs b/src/git/mod.rs index 107aa66..72172d4 100644 --- a/src/git/mod.rs +++ b/src/git/mod.rs @@ -1,6 +1,6 @@ use anyhow::{bail, Context, Result}; use git2::{Repository, Signature, StatusOptions, DiffOptions}; -use std::path::Path; +use std::path::{Path, PathBuf}; pub mod changelog; pub mod commit; @@ -19,13 +19,27 @@ pub struct GitRepo { impl GitRepo { /// Open a git repository pub fn open>(path: P) -> Result { - let path = path.as_ref().canonicalize() - .unwrap_or_else(|_| path.as_ref().to_path_buf()); + let path = path.as_ref(); + + if let Ok(repo) = Repository::discover(&path) { + return Ok(Self { + repo, + path: path.to_path_buf() + }); + } + + if let Ok(repo) = Repository::open(path) { + return Ok(Self { repo, path: path.to_path_buf() }); + } - let repo = Repository::open(&path) - .with_context(|| format!("Failed to open git repository: {:?}", path))?; - - Ok(Self { repo, path }) + // 如果依然失败,给出明确的错误提示 + bail!( + "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.", + path, + path.display() + ) } /// Get repository path