diff --git a/Cargo.toml b/Cargo.toml index 9fc6f9e..aaa99cb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "quicommit" -version = "0.1.7" +version = "0.1.8" edition = "2024" authors = ["Sidney Zhang "] description = "A powerful Git assistant tool with AI-powered commit/tag/changelog generation(alpha version)" diff --git a/src/commands/changelog.rs b/src/commands/changelog.rs index 31351fa..260839b 100644 --- a/src/commands/changelog.rs +++ b/src/commands/changelog.rs @@ -162,8 +162,10 @@ impl ChangelogCommand { if output_path.exists() { let existing = std::fs::read_to_string(&output_path)?; let new_content = if existing.is_empty() { - format!("# Changelog\n\n{}", changelog) - } else { + 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; @@ -181,10 +183,12 @@ impl ChangelogCommand { 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 { - let content = format!("# Changelog\n\n{}", changelog); + let content = format!("{}{}", CHANGELOG_HEADER, changelog); std::fs::write(&output_path, content)?; } diff --git a/src/git/changelog.rs b/src/git/changelog.rs index d09600b..19bcdbc 100644 --- a/src/git/changelog.rs +++ b/src/git/changelog.rs @@ -5,6 +5,15 @@ 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) @@ -378,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(())