删除lib.rs,调整文件结构

This commit is contained in:
HouXiaoxuan
2023-12-28 14:31:15 +08:00
parent 3a4973ce9b
commit 6d697e3e74
18 changed files with 46 additions and 89 deletions

View File

@@ -1,6 +1,5 @@
use clap::{ArgGroup, Parser, Subcommand};
use mit::commands as cmd;
use super::commands as cmd;
/// Rust实现的简易版本的Git用于学习Rust语言
#[derive(Parser)]
#[command(author, version, about, long_about = None)]

View File

@@ -1,6 +1,9 @@
use colored::Colorize;
use crate::{head, models::*, store, utils::util};
use crate::{
models::*,
utils::{head, store, util},
};
// branch error
enum BranchErr {

View File

@@ -1,4 +1,4 @@
use crate::{head, models::*};
use crate::{models::*, utils::head};
use super::status;
@@ -38,7 +38,7 @@ pub fn commit(message: String, allow_empty: bool) {
mod test {
use std::path::Path;
use crate::{commands as cmd, head, models, utils::util};
use crate::{commands as cmd, utils::head, models, utils::util};
#[test]
#[should_panic]

View File

@@ -1,4 +1,4 @@
use crate::{head, models::Commit};
use crate::{models::Commit, utils::head};
use colored::Colorize;
const DEFAULT_LOG_NUMBER: usize = 10;

View File

@@ -1,9 +1,7 @@
use crate::{
commands::{self, status::*},
head,
models::{Commit, Hash},
store::Store,
utils::util,
utils::{head, store, util},
};
enum MergeErr {
@@ -67,7 +65,7 @@ pub fn merge(branch: String) {
head::get_branch_head(&branch)
} else {
// Commit Hash, e.g. a1b2c3d4
let store = Store::new();
let store = store::Store::new();
let commit = store.search(&branch);
if commit.is_none() || !util::is_typeof_commit(commit.clone().unwrap()) {
println!("fatal: 非法的 commit hash: '{}'", branch);

View File

@@ -4,7 +4,10 @@ use std::{
path::PathBuf,
};
use crate::{head, models::*, store::Store, utils::util};
use crate::{
models::*,
utils::{head, store, util},
};
/// 统计[工作区]中相对于target_blobs已删除的文件根据filters进行过滤
fn get_worktree_deleted_files_in_filters(
@@ -64,8 +67,8 @@ pub fn restore_worktree(filter: Option<&Vec<PathBuf>>, target_blobs: &Vec<(PathB
let mut file_paths = util::integrate_paths(&input_paths); //根据用户输入整合存在的文件(绝对路径)
file_paths.extend(deleted_files); //已删除的文件
let index = Index::get_instance();
let store = Store::new();
let index = Index::new();
let store = store::Store::new();
for path in &file_paths {
assert!(path.is_absolute()); // 绝对路径
@@ -165,7 +168,7 @@ pub fn restore(paths: Vec<String>, source: Option<String>, worktree: bool, stage
head::get_branch_head(&src) // "" if not exist
} else {
// [Commit Hash, e.g. a1b2c3d4] || [Wrong Branch Name]
let store = Store::new();
let store = store::Store::new();
let commit = store.search(&src);
if commit.is_none() || !util::is_typeof_commit(commit.clone().unwrap()) {
println!("fatal: 非法的 commit hash: '{}'", src);
@@ -214,8 +217,7 @@ pub fn restore(paths: Vec<String>, source: Option<String>, worktree: bool, stage
mod test {
use std::fs;
//TODO 写测试!
use crate::commands::status;
use crate::{commands as cmd, models::Index, utils::util};
use crate::{commands as cmd, commands::status, models::Index, utils::util};
use std::path::PathBuf;
#[test]

View File

@@ -1,6 +1,5 @@
use crate::{
head,
head::Head,
utils::head,
models::{Commit, Index},
utils::util,
};
@@ -139,10 +138,10 @@ pub fn changes_to_be_staged() -> Changes {
pub fn status() {
util::check_repo_exist();
match head::current_head() {
Head::Detached(commit) => {
head::Head::Detached(commit) => {
println!("HEAD detached at {}", commit[0..7].to_string());
}
Head::Branch(branch) => {
head::Head::Branch(branch) => {
println!("On branch {}", branch);
}
}

View File

@@ -1,9 +1,9 @@
use colored::Colorize;
use crate::{
head::{self},
utils::head,
models::{Commit, Hash},
store::Store,
utils::store,
utils::util,
};
@@ -41,7 +41,7 @@ fn switch_to(branch: String, detach: bool) -> Result<(), SwitchErr> {
return Err(SwitchErr::NoClean);
}
let store = Store::new();
let store = store::Store::new();
if head::list_local_branches().contains(&branch) {
// 切到分支
let branch_commit = head::get_branch_head(&branch);

View File

@@ -1,6 +0,0 @@
// 不使用lib.rs的话就无法在tests里引用到src中的模块
pub mod commands;
pub mod head;
pub mod models;
pub mod store;
pub mod utils;

View File

@@ -1,8 +1,10 @@
use mit::models::Index;
mod cli;
mod commands;
mod models;
mod utils;
fn main() {
color_backtrace::install(); // colorize backtrace
cli::handle_command();
Index::get_instance().save(); //兜底save
models::Index::get_instance().save(); //兜底save
}

View File

@@ -1,4 +1,7 @@
use crate::{models::Hash, store::Store, utils::util};
use crate::{
models::Hash,
utils::{store, util},
};
use std::{fs, path::Path};
/**Blob<br>
@@ -21,14 +24,14 @@ impl Blob {
}
pub fn load(hash: &String) -> Blob {
let s = Store::new();
let s = store::Store::new();
let data = s.load(hash);
Blob { hash: hash.clone(), data }
}
/// 写入文件;优化:文件已存在时不做操作
pub fn save(&self) {
let s = Store::new();
let s = store::Store::new();
if !s.contains(&self.hash) {
let hash = s.save(&self.data);
assert_eq!(hash, self.hash);

View File

@@ -2,7 +2,7 @@ use std::time::SystemTime;
use serde::{Deserialize, Serialize};
use crate::{store, utils::util};
use crate::utils::{store, util};
use super::*;
/*Commit

View File

@@ -55,7 +55,7 @@ pub struct Index {
impl Index {
/// 从index文件加载
fn new() -> Index {
pub fn new() -> Index {
let mut index = Index::default();
index.load();
return index;

View File

@@ -2,7 +2,7 @@ use std::{collections::HashSet, path::PathBuf};
use serde::{Deserialize, Serialize};
use crate::{store, utils::util};
use crate::utils::{store, util};
use super::{Hash, Index};
/*Tree

View File

@@ -113,7 +113,8 @@ pub fn change_head_to_commit(commit_hash: &String) {
#[cfg(test)]
mod test {
use crate::{head::update_branch, utils::util};
use crate::utils::util;
use crate::utils::head;
#[test]
fn test_edit_branch() {
@@ -134,8 +135,8 @@ mod test {
util::setup_test_with_clean_mit();
let branch_one = "test_branch".to_string() + &rand::random::<u32>().to_string();
let branch_two = "test_branch".to_string() + &rand::random::<u32>().to_string();
update_branch(&branch_one, &"1234567890".to_string());
update_branch(&branch_two, &"1234567890".to_string());
head::update_branch(&branch_one, &"1234567890".to_string());
head::update_branch(&branch_two, &"1234567890".to_string());
let branches = super::list_local_branches();
assert!(branches.contains(&branch_one));
@@ -146,7 +147,7 @@ mod test {
fn test_change_head_to_branch() {
util::setup_test_with_clean_mit();
let branch_name = "test_branch".to_string() + &rand::random::<u32>().to_string();
update_branch(&branch_name, &"1234567890".to_string());
head::update_branch(&branch_name, &"1234567890".to_string());
super::change_head_to_branch(&branch_name);
assert!(
match super::current_head() {

View File

@@ -1 +1,3 @@
pub mod util;
pub mod head;
pub mod store;

View File

@@ -2,7 +2,7 @@ use std::path::PathBuf;
use crate::models::Hash;
use super::utils::util;
use super::util;
/// 管理.mit仓库的读写
pub struct Store {

View File

@@ -1,46 +0,0 @@
use mit::utils::util;
use sha1::{Digest, Sha1};
use std::fs::File;
use std::io::{BufRead, BufReader, Error, Write};
#[test]
fn test_hash() {
let mut hasher = Sha1::new();
hasher.update(String::from("hello world"));
let result = format!("{:x}", hasher.finalize());
println!("{}", result);
println!("{}", util::calc_hash(&String::from("hello world")));
}
#[test]
fn test_write() -> Result<(), Error> {
util::setup_test_with_clean_mit();
let path = "lines.txt";
//create会截断文件
let mut output = File::create(path)?; // ? 用于传播错误
write!(output, "Rust\nWrite\nRead4")?;
Ok(())
}
#[test]
fn test_read() -> Result<(), Error> {
util::setup_test_with_clean_mit();
let path = "lines.txt";
util::ensure_test_file(path.as_ref(), None);
let input = File::open(path)?;
let buffered = BufReader::new(input);
for line in buffered.lines() {
println!("{}", line?);
}
Ok(())
}
#[test]
fn test_string() {
let mut s = String::from("Hello");
s.push_str(", world!");
s += "2";
s.push('!');
println!("{}", s);
}