Skip to content

Commit cfba529

Browse files
committed
feat: add copyright check functionality
Added copyright check module that automatically verifies copyright and license headers in staged files during commit process. The system checks for copyright notices, validates copyright years, and ensures license declarations are present. For new files, it examines staged content while for modified files it checks working directory content. Users are prompted to confirm continuation if copyright issues are detected. The implementation includes file type detection, content extraction from git staging area, and comprehensive copyright pattern matching. Uses chrono crate for current year comparison to ensure copyright dates are up-to-date. Log: Added automated copyright checking during commit process Influence: 1. Test copyright check with files containing valid copyright headers 2. Verify detection of missing copyright notices in new files 3. Test copyright year validation with outdated years 4. Check license declaration detection for various license types 5. Verify user prompt behavior when copyright issues are found 6. Test skip functionality when using amend mode 7. Validate file type detection for different file extensions 8. Test content extraction from both staged and working directory files
1 parent 3c0ebad commit cfba529

6 files changed

Lines changed: 566 additions & 6 deletions

File tree

Cargo.lock

Lines changed: 119 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ anyhow = "1.0"
1818
async-trait = "0.1"
1919
config = "0.13"
2020
regex = "1.10"
21+
chrono = "0.4"
2122
textwrap = "0.16"
2223
clap = { version = "4.4", features = ["derive"] }
2324
clap_complete = { version = "4.5", features = ["unstable-dynamic"] }

src/commit.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use regex::Regex;
22
use crate::ai_service;
33
use crate::config;
44
use crate::git;
5+
use crate::review;
6+
use crate::copyright_check;
57

68
/// 从提交消息中提取 Change-Id
79
fn extract_change_id(message: &str) -> Option<String> {
@@ -718,9 +720,8 @@ impl CommitMessage {
718720
}
719721
}
720722

721-
use crate::review;
722723
use dialoguer::Confirm;
723-
use log::{debug, info};
724+
use log::{debug, info, warn};
724725
use std::process::Command;
725726

726727
pub async fn generate_commit_message(
@@ -786,6 +787,38 @@ pub async fn generate_commit_message(
786787

787788
let config = config::Config::load()?;
788789

790+
// 执行版权检查(对于 amend 模式,我们跳过检查)
791+
if !amend {
792+
info!("正在进行版权检查...");
793+
794+
// 优先使用 AI 检查,如果失败则回退到硬编码检查
795+
let check_result = match copyright_check::check_copyright_with_ai(&config).await {
796+
Ok(result) => {
797+
info!("AI 版权检查完成");
798+
result
799+
}
800+
Err(e) => {
801+
warn!("AI 版权检查失败,回退到硬编码检查: {}", e);
802+
copyright_check::check_copyright()?
803+
}
804+
};
805+
806+
let formatted = copyright_check::format_copyright_result(&check_result);
807+
println!("\n{}\n", formatted);
808+
809+
// 如果有版权问题,询问用户是否继续
810+
if check_result.has_issues {
811+
if !Confirm::with_theme(&dialoguer::theme::ColorfulTheme::default())
812+
.with_prompt("发现版权问题,是否继续提交?")
813+
.default(false)
814+
.interact()?
815+
{
816+
println!("已取消提交");
817+
return Ok(());
818+
}
819+
}
820+
}
821+
789822
// 在确认有差异内容后执行代码审查(对于 amend 模式,我们跳过审查,因为是对已有提交的修改)
790823
if !amend && !no_review && config.ai_review {
791824
info!("正在进行代码审查...");

0 commit comments

Comments
 (0)