fix: 修复 tag/changelog 生成范围,增加 --auto 模式
- fix(get_tags): 使用 find_object+peel_to_commit 支持 annotated tag - fix(changelog): insert_changelog_entry 替换覆盖逻辑,保留已有章节 - feat: parse_changelog_versions 按 semver 降序提取版本 - feat: sort_tags_by_semver 取代纯时间排序 - feat(tag --auto): 优先读 Cargo.toml/pyproject.toml,回退 commit 分析 - feat(changelog): 无 --from 时自动检测 changelog 最高已有版本 - refactor: TagInfo::version_name(), GitRepo::find_tag_by_version()
This commit is contained in:
158
src/git/mod.rs
158
src/git/mod.rs
@@ -862,24 +862,34 @@ impl GitRepo {
|
||||
let name = String::from_utf8_lossy(name);
|
||||
let name = name.strip_prefix("refs/tags/").unwrap_or(&name);
|
||||
|
||||
if let Ok(commit) = self.repo.find_commit(oid) {
|
||||
tags.push(TagInfo {
|
||||
name: name.to_string(),
|
||||
target: oid.to_string(),
|
||||
message: commit.message().unwrap_or("").to_string(),
|
||||
time: commit.time().seconds(),
|
||||
});
|
||||
// Use find_object + peel_to_commit to handle both lightweight
|
||||
// (oid is a commit) and annotated (oid is a tag object) tags.
|
||||
if let Ok(obj) = self.repo.find_object(oid, None) {
|
||||
if let Ok(commit) = obj.peel_to_commit() {
|
||||
tags.push(TagInfo {
|
||||
name: name.to_string(),
|
||||
target: commit.id().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));
|
||||
// Sort tags by semver (descending), then by time
|
||||
crate::git::tag::sort_tags_by_semver(&mut tags);
|
||||
|
||||
Ok(tags)
|
||||
}
|
||||
|
||||
/// Find a tag whose version (stripping 'v' prefix) matches the given version string.
|
||||
pub fn find_tag_by_version(&self, version: &str) -> Option<TagInfo> {
|
||||
let tags = self.get_tags().ok()?;
|
||||
tags.into_iter().find(|t| t.version_name() == version)
|
||||
}
|
||||
|
||||
/// Create a tag
|
||||
pub fn create_tag(&self, name: &str, message: Option<&str>, sign: bool) -> Result<()> {
|
||||
let head = self.repo.head()?;
|
||||
@@ -1071,6 +1081,13 @@ pub struct TagInfo {
|
||||
pub time: i64,
|
||||
}
|
||||
|
||||
impl TagInfo {
|
||||
/// Return the version portion of the tag name (stripping leading 'v' if present).
|
||||
pub fn version_name(&self) -> &str {
|
||||
self.name.strip_prefix('v').unwrap_or(&self.name)
|
||||
}
|
||||
}
|
||||
|
||||
/// Repository status summary
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct StatusSummary {
|
||||
@@ -1441,3 +1458,126 @@ pub struct ConfigDiff {
|
||||
pub left: String,
|
||||
pub right: String,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use git2::Signature;
|
||||
use tempfile::TempDir;
|
||||
|
||||
fn init_test_repo() -> (TempDir, GitRepo) {
|
||||
let dir = TempDir::new().unwrap();
|
||||
let repo = git2::Repository::init(dir.path()).unwrap();
|
||||
|
||||
// Configure git user
|
||||
let mut config = repo.config().unwrap();
|
||||
config.set_str("user.name", "Test User").unwrap();
|
||||
config.set_str("user.email", "test@example.com").unwrap();
|
||||
|
||||
// Create an initial commit (needed for tags)
|
||||
let sig = Signature::now("Test User", "test@example.com").unwrap();
|
||||
let tree_id = {
|
||||
let mut index = repo.index().unwrap();
|
||||
index.write_tree().unwrap()
|
||||
};
|
||||
let tree = repo.find_tree(tree_id).unwrap();
|
||||
repo.commit(Some("HEAD"), &sig, &sig, "initial commit", &tree, &[])
|
||||
.unwrap();
|
||||
|
||||
let git_repo = GitRepo::open(dir.path()).unwrap();
|
||||
(dir, git_repo)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_tags_returns_annotated_tags() {
|
||||
let (_dir, repo) = init_test_repo();
|
||||
let sig = Signature::now("Test User", "test@example.com").unwrap();
|
||||
let head = repo.repo.head().unwrap().peel_to_commit().unwrap();
|
||||
|
||||
// Create an annotated tag (what QuiCommit creates by default)
|
||||
repo.repo
|
||||
.tag("v0.2.0", head.as_object(), &sig, "Release v0.2.0", false)
|
||||
.unwrap();
|
||||
|
||||
let tags = repo.get_tags().unwrap();
|
||||
let tag_names: Vec<&str> = tags.iter().map(|t| t.name.as_str()).collect();
|
||||
|
||||
assert!(
|
||||
tag_names.contains(&"v0.2.0"),
|
||||
"annotated tag v0.2.0 should appear in tags list, got: {:?}",
|
||||
tag_names
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_tags_returns_lightweight_tags() {
|
||||
let (_dir, repo) = init_test_repo();
|
||||
let head = repo.repo.head().unwrap().peel_to_commit().unwrap();
|
||||
|
||||
// Create a lightweight tag
|
||||
repo.repo.tag("v0.1.0", head.as_object(), &repo.repo.signature().unwrap(), "", false)
|
||||
.unwrap();
|
||||
|
||||
let tags = repo.get_tags().unwrap();
|
||||
let tag_names: Vec<&str> = tags.iter().map(|t| t.name.as_str()).collect();
|
||||
|
||||
assert!(
|
||||
tag_names.contains(&"v0.1.0"),
|
||||
"lightweight tag v0.1.0 should appear in tags list, got: {:?}",
|
||||
tag_names
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_tags_returns_mixed_annotated_and_lightweight() {
|
||||
let (_dir, repo) = init_test_repo();
|
||||
let sig = Signature::now("Test User", "test@example.com").unwrap();
|
||||
let head = repo.repo.head().unwrap().peel_to_commit().unwrap();
|
||||
|
||||
// Create annotated tag
|
||||
repo.repo
|
||||
.tag("v0.2.0", head.as_object(), &sig, "annotated", false)
|
||||
.unwrap();
|
||||
|
||||
// Create lightweight tag
|
||||
repo.repo
|
||||
.tag("v0.1.0", head.as_object(), &repo.repo.signature().unwrap(), "", false)
|
||||
.unwrap();
|
||||
|
||||
let tags = repo.get_tags().unwrap();
|
||||
let tag_names: Vec<&str> = tags.iter().map(|t| t.name.as_str()).collect();
|
||||
|
||||
assert!(
|
||||
tag_names.contains(&"v0.2.0"),
|
||||
"annotated tag should be present, got: {:?}",
|
||||
tag_names
|
||||
);
|
||||
assert!(
|
||||
tag_names.contains(&"v0.1.0"),
|
||||
"lightweight tag should be present, got: {:?}",
|
||||
tag_names
|
||||
);
|
||||
assert_eq!(tags.len(), 2, "should have exactly 2 tags");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_tags_target_points_to_commit_not_tag_object() {
|
||||
let (_dir, repo) = init_test_repo();
|
||||
let sig = Signature::now("Test User", "test@example.com").unwrap();
|
||||
let head = repo.repo.head().unwrap().peel_to_commit().unwrap();
|
||||
let expected_commit_id = head.id().to_string();
|
||||
|
||||
// Create annotated tag
|
||||
repo.repo
|
||||
.tag("v0.2.0", head.as_object(), &sig, "annotated", false)
|
||||
.unwrap();
|
||||
|
||||
let tags = repo.get_tags().unwrap();
|
||||
let tag = tags.iter().find(|t| t.name == "v0.2.0").unwrap();
|
||||
|
||||
assert_eq!(
|
||||
tag.target, expected_commit_id,
|
||||
"tag.target should be the commit OID, not the tag object OID"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user