From 0c7d2ad51853d0848ceec628d7337dcd32fbdb3a Mon Sep 17 00:00:00 2001 From: SidneyZhang Date: Fri, 20 Mar 2026 18:05:33 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=A7=BB=E9=99=A4=E6=9C=AA?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E7=9A=84=E4=BB=A3=E7=A0=81=E5=92=8C=E6=B3=A8?= =?UTF-8?q?=E9=87=8A=E6=8E=89=E7=9A=84=E8=BE=85=E5=8A=A9=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/commands/commit.rs | 42 +++++------ src/commands/config.rs | 2 +- src/commands/init.rs | 2 +- src/config/manager.rs | 162 ++++++++++++++++++++--------------------- src/config/mod.rs | 28 +++---- src/git/mod.rs | 5 ++ 6 files changed, 123 insertions(+), 118 deletions(-) diff --git a/src/commands/commit.rs b/src/commands/commit.rs index e284298..5685669 100644 --- a/src/commands/commit.rs +++ b/src/commands/commit.rs @@ -373,26 +373,26 @@ impl CommitCommand { } } -// Helper trait for optional builder methods -trait CommitBuilderExt { - fn scope_opt(self, scope: Option) -> Self; - fn body_opt(self, body: Option) -> Self; -} +// // Helper trait for optional builder methods +// trait CommitBuilderExt { +// fn scope_opt(self, scope: Option) -> Self; +// fn body_opt(self, body: Option) -> Self; +// } -impl CommitBuilderExt for CommitBuilder { - fn scope_opt(self, scope: Option) -> Self { - if let Some(s) = scope { - self.scope(s) - } else { - self - } - } +// impl CommitBuilderExt for CommitBuilder { +// fn scope_opt(self, scope: Option) -> Self { +// if let Some(s) = scope { +// self.scope(s) +// } else { +// self +// } +// } - fn body_opt(self, body: Option) -> Self { - if let Some(b) = body { - self.body(b) - } else { - self - } - } -} +// fn body_opt(self, body: Option) -> Self { +// if let Some(b) = body { +// self.body(b) +// } else { +// self +// } +// } +// } diff --git a/src/commands/config.rs b/src/commands/config.rs index a54dbd0..f71ea07 100644 --- a/src/commands/config.rs +++ b/src/commands/config.rs @@ -750,7 +750,7 @@ impl ConfigCommand { let manager = self.get_manager(config_path)?; let toml = manager.export()?; - let export_content = if let Some(path) = output { + let export_content = if let Some(_path) = output { let pwd = if let Some(p) = password { p.to_string() } else { diff --git a/src/commands/init.rs b/src/commands/init.rs index 280e045..208340c 100644 --- a/src/commands/init.rs +++ b/src/commands/init.rs @@ -240,7 +240,7 @@ impl InitCommand { .or_else(|_| std::env::var(format!("QUICOMMIT_{}_API_KEY", provider.to_uppercase()))) .ok(); - if let Some(key) = env_key { + if let Some(_key) = env_key { println!("\n{} {}", "✓".green(), "Found API key in environment variable.".green()); None } else if keyring_available { diff --git a/src/config/manager.rs b/src/config/manager.rs index d3d77f2..60b10d5 100644 --- a/src/config/manager.rs +++ b/src/config/manager.rs @@ -1,7 +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 @@ -64,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 { @@ -118,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> { @@ -170,10 +170,10 @@ 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 @@ -188,10 +188,10 @@ impl ConfigManager { } } - /// 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) - } + // /// 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) + // } /// Remove a token from a profile pub fn remove_token_from_profile(&mut self, profile_name: &str, service: &str) -> Result<()> { @@ -204,20 +204,20 @@ impl ConfigManager { } } - /// List all tokens in a profile - pub fn list_profile_tokens(&self, profile_name: &str) -> Option> { - self.config.profiles.get(profile_name).map(|p| p.tokens.keys().collect()) - } + // /// List all tokens in a profile + // pub fn list_profile_tokens(&self, profile_name: &str) -> Option> { + // 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<()> { @@ -229,26 +229,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 { - &self.config.repo_profiles - } + // /// List repository profile mappings + // pub fn list_repo_profiles(&self) -> &HashMap { + // &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 { @@ -383,31 +383,31 @@ impl ConfigManager { &self.keyring } - /// Configure LLM provider with all settings - pub fn configure_llm(&mut self, provider: String, model: Option, base_url: Option, api_key: Option<&str>) -> Result<()> { - self.set_llm_provider(provider.clone()); + // /// Configure LLM provider with all settings + // pub fn configure_llm(&mut self, provider: String, model: Option, base_url: Option, api_key: Option<&str>) -> Result<()> { + // self.set_llm_provider(provider.clone()); - if let Some(m) = model { - self.set_llm_model(m); - } + // if let Some(m) = model { + // self.set_llm_model(m); + // } - self.set_llm_base_url(base_url); + // self.set_llm_base_url(base_url); - if let Some(key) = api_key { - if provider_needs_api_key(&provider) { - self.set_api_key(key)?; - } - } + // if let Some(key) = api_key { + // if provider_needs_api_key(&provider) { + // self.set_api_key(key)?; + // } + // } - Ok(()) - } + // 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) { @@ -415,10 +415,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) { @@ -428,10 +428,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) { @@ -441,10 +441,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) { @@ -454,10 +454,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) { diff --git a/src/config/mod.rs b/src/config/mod.rs index b33e5c3..6061b10 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -9,7 +9,7 @@ pub mod profile; pub use profile::{ GitProfile, TokenConfig, TokenType, - UsageStats, ProfileComparison + ProfileComparison }; /// Application configuration @@ -505,18 +505,18 @@ 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); + // } + // self.repo_profiles.insert(repo_path, profile_name); + // Ok(()) + // } } diff --git a/src/git/mod.rs b/src/git/mod.rs index 5a98475..8df4e56 100644 --- a/src/git/mod.rs +++ b/src/git/mod.rs @@ -642,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) } @@ -832,6 +836,7 @@ pub struct TagInfo { pub name: String, pub target: String, pub message: String, + pub time: i64, } /// Repository status summary