Skip to content

Commit

Permalink
删除lib.rs,调整文件结构
Browse files Browse the repository at this point in the history
  • Loading branch information
Hou-Xiaoxuan committed Dec 28, 2023
1 parent 3a4973c commit 6d697e3
Show file tree
Hide file tree
Showing 18 changed files with 46 additions and 89 deletions.
3 changes: 1 addition & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
5 changes: 4 additions & 1 deletion src/commands/branch.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/commit.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{head, models::*};
use crate::{models::*, utils::head};

use super::status;

Expand Down Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion src/commands/log.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{head, models::Commit};
use crate::{models::Commit, utils::head};
use colored::Colorize;

const DEFAULT_LOG_NUMBER: usize = 10;
Expand Down
6 changes: 2 additions & 4 deletions src/commands/merge.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use crate::{
commands::{self, status::*},
head,
models::{Commit, Hash},
store::Store,
utils::util,
utils::{head, store, util},
};

enum MergeErr {
Expand Down Expand Up @@ -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);
Expand Down
14 changes: 8 additions & 6 deletions src/commands/restore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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()); // 绝对路径
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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]
Expand Down
7 changes: 3 additions & 4 deletions src/commands/status.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{
head,
head::Head,
utils::head,
models::{Commit, Index},
utils::util,
};
Expand Down Expand Up @@ -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);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/commands/switch.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use colored::Colorize;

use crate::{
head::{self},
utils::head,
models::{Commit, Hash},
store::Store,
utils::store,
utils::util,
};

Expand Down Expand Up @@ -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);
Expand Down
6 changes: 0 additions & 6 deletions src/lib.rs

This file was deleted.

8 changes: 5 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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
}
9 changes: 6 additions & 3 deletions src/models/blob.rs
Original file line number Diff line number Diff line change
@@ -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>
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/models/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/models/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/models/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions src/head.rs → src/utils/head.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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));
Expand All @@ -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() {
Expand Down
2 changes: 2 additions & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
pub mod util;
pub mod head;
pub mod store;
2 changes: 1 addition & 1 deletion src/store.rs → src/utils/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::path::PathBuf;

use crate::models::Hash;

use super::utils::util;
use super::util;

/// 管理.mit仓库的读写
pub struct Store {
Expand Down
46 changes: 0 additions & 46 deletions tests/test.rs
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 6d697e3

Please sign in to comment.