feat(changelog): 使用统一的 CHANGELOG_HEADER 常量生成标准化头部

This commit is contained in:
2026-02-28 15:29:07 +08:00
parent 04410ea9e7
commit c2e30317da
3 changed files with 24 additions and 17 deletions

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "quicommit" name = "quicommit"
version = "0.1.7" version = "0.1.8"
edition = "2024" edition = "2024"
authors = ["Sidney Zhang <zly@lyzhang.me>"] authors = ["Sidney Zhang <zly@lyzhang.me>"]
description = "A powerful Git assistant tool with AI-powered commit/tag/changelog generation(alpha version)" description = "A powerful Git assistant tool with AI-powered commit/tag/changelog generation(alpha version)"

View File

@@ -162,8 +162,10 @@ impl ChangelogCommand {
if output_path.exists() { if output_path.exists() {
let existing = std::fs::read_to_string(&output_path)?; let existing = std::fs::read_to_string(&output_path)?;
let new_content = if existing.is_empty() { let new_content = if existing.is_empty() {
format!("# Changelog\n\n{}", changelog) format!("{}{}", CHANGELOG_HEADER, changelog)
} else { } 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 lines: Vec<&str> = existing.lines().collect();
let mut header_end = 0; let mut header_end = 0;
@@ -181,10 +183,12 @@ impl ChangelogCommand {
let rest = lines[header_end..].join("\n"); let rest = lines[header_end..].join("\n");
format!("{}\n{}\n{}", header, changelog, rest) format!("{}\n{}\n{}", header, changelog, rest)
} else {
format!("{}{}", CHANGELOG_HEADER, changelog)
}; };
std::fs::write(&output_path, new_content)?; std::fs::write(&output_path, new_content)?;
} else { } else {
let content = format!("# Changelog\n\n{}", changelog); let content = format!("{}{}", CHANGELOG_HEADER, changelog);
std::fs::write(&output_path, content)?; std::fs::write(&output_path, content)?;
} }

View File

@@ -5,6 +5,15 @@ use std::collections::HashMap;
use std::fs; use std::fs;
use std::path::Path; 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 /// Changelog generator
pub struct ChangelogGenerator { pub struct ChangelogGenerator {
format: ChangelogFormat, format: ChangelogFormat,
@@ -109,9 +118,10 @@ impl ChangelogGenerator {
}; };
let new_content = if existing.is_empty() { let new_content = if existing.is_empty() {
format!("# Changelog\n\n{}", entry) format!("{}{}", CHANGELOG_HEADER, entry)
} else { } else if existing.starts_with(CHANGELOG_HEADER) {
// Find position after header format!("{}{}", CHANGELOG_HEADER, entry)
} else if existing.starts_with("# Changelog") {
let lines: Vec<&str> = existing.lines().collect(); let lines: Vec<&str> = existing.lines().collect();
let mut header_end = 0; let mut header_end = 0;
@@ -129,6 +139,8 @@ impl ChangelogGenerator {
let rest = lines[header_end..].join("\n"); let rest = lines[header_end..].join("\n");
format!("{}\n{}\n{}", header, entry, rest) format!("{}\n{}\n{}", header, entry, rest)
} else {
format!("{}{}", CHANGELOG_HEADER, entry)
}; };
fs::write(changelog_path, new_content) fs::write(changelog_path, new_content)
@@ -378,16 +390,7 @@ pub fn init_changelog(path: &Path) -> Result<()> {
anyhow::bail!("Changelog already exists at {:?}", path); anyhow::bail!("Changelog already exists at {:?}", path);
} }
let content = r#"# Changelog fs::write(path, CHANGELOG_HEADER)
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)
.with_context(|| format!("Failed to create changelog: {:?}", path))?; .with_context(|| format!("Failed to create changelog: {:?}", path))?;
Ok(()) Ok(())