From b560a3f3028386f35e7050ff1e49b87ba638bd6a Mon Sep 17 00:00:00 2001 From: HouXiaoxuan Date: Thu, 21 Dec 2023 02:03:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=87=BD=E6=95=B0=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/head.rs | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/head.rs b/src/head.rs index 90c4398..2de4fcc 100644 --- a/src/head.rs +++ b/src/head.rs @@ -25,7 +25,7 @@ fn update_branch_head(branch_name: &String, commit_hash: &String) { std::fs::write(branch, commit_hash).expect("无法写入branch"); } -fn get_branch_head(branch_name: &String) -> std::option::Option { +fn get_branch_head(branch_name: &String) -> String { // 返回当前分支的commit hash let mut branch = util::get_storage_path().unwrap(); branch.push("refs"); @@ -33,9 +33,9 @@ fn get_branch_head(branch_name: &String) -> std::option::Option { branch.push(branch_name); if branch.exists() { let commit_hash = std::fs::read_to_string(branch).expect("无法读取branch"); - Some(commit_hash) + commit_hash } else { - None + "".to_string() // 分支不存在或者没有commit } } @@ -44,7 +44,7 @@ pub fn current_head_commit() -> String { let head = current_head(); match head { Head::Branch(branch_name) => { - let commit_hash = get_branch_head(&branch_name).unwrap(); + let commit_hash = get_branch_head(&branch_name); commit_hash } Head::Detached(commit_hash) => commit_hash, @@ -87,7 +87,7 @@ pub fn list_local_branches() -> Vec { pub fn change_head_to_branch(branch_name: &String) { let mut head = util::get_storage_path().unwrap(); head.push("HEAD"); - let branch_head = get_branch_head(branch_name).unwrap(); + let branch_head = get_branch_head(branch_name); std::fs::write(head, format!("ref: refs/heads/{}", branch_name)).expect("无法写入HEAD"); update_head_commit(&branch_head); } @@ -108,13 +108,13 @@ mod test { util::setup_test_with_mit(); let branch_name = "test_branch".to_string() + &rand::random::().to_string(); let branch_head = super::get_branch_head(&branch_name); - assert!(branch_head.is_none()); + assert!(branch_head.is_empty()); let commit_hash = "1234567890".to_string(); super::update_branch_head(&branch_name, &commit_hash); let branch_head = super::get_branch_head(&branch_name); - assert!(branch_head.is_some()); - assert!(branch_head.unwrap() == commit_hash); + assert!(!branch_head.is_empty()); + assert!(branch_head == commit_hash); } #[test] @@ -158,4 +158,15 @@ mod test { "当前不在分支上" ); } + + #[test] + fn test_update_branch_head() { + util::setup_test_with_mit(); + let branch_name = "test_branch".to_string() + &rand::random::().to_string(); + let commit_hash = "1234567890".to_string(); + super::update_branch_head(&branch_name, &commit_hash); + let branch_head = super::get_branch_head(&branch_name); + assert!(!branch_head.is_empty()); + assert!(branch_head == commit_hash); + } }