增加hash类型判断

This commit is contained in:
HouXiaoxuan
2023-12-22 13:23:10 +08:00
parent 7b91fad41f
commit 0ab07cc4c4
3 changed files with 47 additions and 3 deletions

View File

@@ -4,6 +4,7 @@ use crate::{
head,
models::{commit::Commit, object::Hash},
store,
utils::util,
};
// branch error
@@ -30,12 +31,12 @@ fn search_hash(commit_hash: Hash) -> Option<Hash> {
fn create_branch(branch_name: String, _base_commit: Hash) -> Result<(), BranchErr> {
// 找到正确的base_commit_hash
let base_commit = search_hash(_base_commit.clone());
if base_commit.is_none() {
if base_commit.is_none() || util::check_object_type(base_commit.clone().unwrap()) != util::ObjectType::Commit {
println!("fatal: 非法的 commit: '{}'", _base_commit);
return Err(BranchErr::InvalidObject);
}
let base_commit = Commit::load(&base_commit.unwrap()); // TODO 这里会直接panic可以优化一下错误处理流程
let base_commit = Commit::load(&base_commit.unwrap());
let exist_branchs = head::list_local_branches();
if exist_branchs.contains(&branch_name) {

View File

@@ -6,6 +6,7 @@ use crate::{
head::{self},
models::{commit::Commit, object::Hash},
store::Store,
utils::util,
};
use super::{
@@ -51,10 +52,11 @@ fn switch_to(branch: String, detach: bool) -> Result<(), SwitchErr> {
println!("切换到分支: '{}'", branch.green())
} else if detach {
let commit = store.search(&branch);
if commit.is_none() {
if commit.is_none() || util::check_object_type(commit.clone().unwrap()) != util::ObjectType::Commit {
println!("fatal: 非法的 commit: '{}'", branch);
return Err(SwitchErr::InvalidObject);
}
// 切到commit
let commit = commit.unwrap();
switch_to_commit(None, commit.clone());