mirror of
https://github.com/MrBeanCpp/MIT.git
synced 2026-03-24 05:51:16 +08:00
优化commit第一次提交,parent为空而不是“”
This commit is contained in:
@@ -17,7 +17,13 @@ pub fn commit(message: String, allow_empty: bool) {
|
||||
let current_head = head::current_head();
|
||||
let current_commit_hash = head::current_head_commit();
|
||||
|
||||
let mut commit = commit::Commit::new(&index, vec![current_commit_hash.clone()], message.clone());
|
||||
let mut commit = {
|
||||
if current_commit_hash.is_empty() {
|
||||
commit::Commit::new(&index, vec![], message.clone())
|
||||
} else {
|
||||
commit::Commit::new(&index, vec![current_commit_hash.clone()], message.clone())
|
||||
}
|
||||
};
|
||||
let commit_hash = commit.save();
|
||||
head::update_head_commit(&commit_hash);
|
||||
|
||||
@@ -35,7 +41,9 @@ pub fn commit(message: String, allow_empty: bool) {
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::utils::util;
|
||||
use std::path::Path;
|
||||
|
||||
use crate::{commands, head, models, utils::util};
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
@@ -44,4 +52,21 @@ mod test {
|
||||
|
||||
super::commit("".to_string(), false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_commit() {
|
||||
util::setup_test_with_clean_mit();
|
||||
let test_file = "a.txt";
|
||||
let head_one = head::current_head_commit();
|
||||
assert!(head_one.is_empty());
|
||||
|
||||
util::ensure_test_file(&Path::new(test_file), "test content".into());
|
||||
commands::commit::commit("test commit 1".to_string(), true);
|
||||
let head_two = head::current_head_commit();
|
||||
assert!(head_two.len() > 0);
|
||||
|
||||
let commit = models::commit::Commit::load(&head_two);
|
||||
assert!(commit.get_parent_hash().len() == 0);
|
||||
assert!(commit.get_message() == "test commit 1");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ use std::time::SystemTime;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::store;
|
||||
use crate::{store, utils::util};
|
||||
|
||||
use super::{index::Index, object::Hash, tree::Tree};
|
||||
/*Commit
|
||||
@@ -26,6 +26,9 @@ impl Commit {
|
||||
pub fn get_hash(&self) -> String {
|
||||
self.hash.clone()
|
||||
}
|
||||
pub fn get_date(&self) -> String {
|
||||
util::format_time(&self.date)
|
||||
}
|
||||
pub fn get_tree_hash(&self) -> String {
|
||||
self.tree.clone()
|
||||
}
|
||||
|
||||
@@ -91,7 +91,7 @@ impl Tree {
|
||||
.map(|file| util::to_workdir_relative_path(file))
|
||||
.collect();
|
||||
|
||||
store_path_to_tree(&file_entries, "".to_string().into())
|
||||
store_path_to_tree(&file_entries, "".into())
|
||||
}
|
||||
|
||||
pub fn load(hash: &String) -> Tree {
|
||||
|
||||
Reference in New Issue
Block a user