统一format格式(rustfmt)

This commit is contained in:
mrbeanc
2023-12-21 20:44:15 +08:00
parent af194c3c2c
commit a2e9072951
14 changed files with 61 additions and 68 deletions

View File

@@ -60,10 +60,10 @@ pub fn handle_command() {
Command::Add { files, all, update } => {
add(files, all, update);
}
Command::Rm { files, cached, recursive} => {
Command::Rm { files, cached, recursive } => {
remove(files, cached, recursive).expect("删除失败");
}
Command::Commit { message, allow_empty, } => {
Command::Commit { message, allow_empty } => {
commit(message, allow_empty);
}
}

View File

@@ -1,9 +1,11 @@
use std::env;
use std::path::{Path, PathBuf};
use colored::Colorize;
use crate::models::blob::Blob;
use crate::models::index::{FileMetaData, Index};
use crate::utils::util::{check_repo_exist, get_relative_path, get_working_dir, list_files};
use colored::Colorize;
use std::env;
use std::path::{Path, PathBuf};
// TODO: fatal: ../moj/app.py: '../moj/app.py' is outside repository at 'Git-Rust'
pub fn add(files: Vec<String>, all: bool, mut update: bool) {
@@ -19,6 +21,7 @@ pub fn add(files: Vec<String>, all: bool, mut update: bool) {
dot = false;
update = false;
} else if update {
// update 优先级次之
dot = false;
}
@@ -29,9 +32,9 @@ pub fn add(files: Vec<String>, all: bool, mut update: bool) {
println!("{}", "'.'代表了当前目录".bright_green());
env::current_dir().unwrap()
} else {
panic!("不应该运行到这里");
panic!();
};
println!("Working on [{}]\n", path.to_str().unwrap().bright_blue());
files = list_files(&path).unwrap();
if update {

View File

@@ -1,5 +1,5 @@
pub mod init;
pub mod add;
pub mod commit;
pub mod init;
pub mod remove;
pub mod status;
pub mod remove;

View File

@@ -1,9 +1,9 @@
use std::{fs, io};
use std::path::PathBuf;
use colored::Colorize;
use crate::models::index::Index;
use crate::utils::util;
use crate::utils::util::check_repo_exist;
use colored::Colorize;
use std::path::PathBuf;
use std::{fs, io};
/// 从暂存区&|工作区删除文件
pub fn remove(files: Vec<String>, cached: bool, recursive: bool) -> io::Result<()> {
@@ -15,7 +15,8 @@ pub fn remove(files: Vec<String>, cached: bool, recursive: bool) -> io::Result<(
println!("Warning: {} not exist", file.red());
continue;
}
if !index.contains(&path) { //不能删除未跟踪的文件
if !index.contains(&path) {
//不能删除未跟踪的文件
println!("Warning: {} not tracked", file.red());
continue;
}
@@ -41,4 +42,4 @@ pub fn remove(files: Vec<String>, cached: bool, recursive: bool) -> io::Result<(
println!("removed [{}]", file.bright_green());
}
Ok(())
}
}

View File

@@ -25,10 +25,8 @@ fn update_branch_head(branch_name: &String, commit_hash: &String) {
branch.push("refs");
branch.push("heads");
branch.push(branch_name);
std::fs::write(&branch, commit_hash).expect(&format!(
"无法写入branch in {:?} with {}",
branch, commit_hash
));
std::fs::write(&branch, commit_hash)
.expect(&format!("无法写入branch in {:?} with {}", branch, commit_hash));
}
fn get_branch_head(branch_name: &String) -> String {

View File

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

View File

@@ -27,10 +27,7 @@ impl Blob {
pub fn load(hash: &String) -> Blob {
let s = Store::new();
let data = s.load(hash);
Blob {
hash: hash.clone(),
data,
}
Blob { hash: hash.clone(), data }
}
/// 写入文件;优化:文件已存在时不做操作

View File

@@ -2,12 +2,12 @@ use crate::models::blob::Blob;
use crate::models::object::Hash;
use crate::utils::util;
use crate::utils::util::get_relative_path;
use colored::Colorize;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};
use std::time::SystemTime;
use colored::Colorize;
// 文件元数据结构
#[derive(Serialize, Deserialize, Debug, Clone)]
@@ -85,11 +85,6 @@ impl Index {
&self.get_hash(file).unwrap_or_default() == hash
}
// 获取所有文件元数据
fn get_all(&self) -> &HashMap<PathBuf, FileMetaData> {
&self.entries
}
pub fn contains(&self, path: &Path) -> bool {
let path = Index::preprocess_path(path);
self.entries.contains_key(&path)

View File

@@ -1,5 +1,5 @@
pub mod commit;
pub mod blob;
pub mod tree;
pub mod commit;
pub mod index;
pub mod object;
pub mod index;
pub mod tree;

View File

@@ -1 +1 @@
pub type Hash = String;
pub type Hash = String;

View File

@@ -38,10 +38,7 @@ fn store_path_to_tree(path_entries: &Vec<PathBuf>, current_root: PathBuf) -> Tre
};
entry
};
let mut tree = Tree {
hash: "".to_string(),
entries: Vec::new(),
};
let mut tree = Tree { hash: "".to_string(), entries: Vec::new() };
let mut processed_path: HashMap<String, bool> = HashMap::new();
for path in path_entries.iter() {
// 判断是不是直接在根目录下
@@ -149,10 +146,7 @@ impl Tree {
sub_blobs
.iter()
.map(|(path, blob_hash)| {
(
PathBuf::from(entry.name.clone()).join(path),
blob_hash.clone(),
)
(PathBuf::from(entry.name.clone()).join(path), blob_hash.clone())
})
.collect::<Vec<(PathBuf, Hash)>>()
.as_mut(),
@@ -177,14 +171,8 @@ mod test {
for test_file in vec!["b.txt", "mit_src/a.txt"] {
let test_file = PathBuf::from(test_file);
util::ensure_test_file(&test_file, None);
index.add(
test_file.clone(),
FileMetaData::new(&Blob::new(&test_file), &test_file),
);
index.add(
test_file.clone(),
FileMetaData::new(&Blob::new(&test_file), &test_file),
);
index.add(test_file.clone(), FileMetaData::new(&Blob::new(&test_file), &test_file));
index.add(test_file.clone(), FileMetaData::new(&Blob::new(&test_file), &test_file));
}
let tree = super::Tree::new(&index);
@@ -200,10 +188,7 @@ mod test {
for test_file in test_files.clone() {
let test_file = PathBuf::from(test_file);
util::ensure_test_file(&test_file, None);
index.add(
test_file.clone(),
FileMetaData::new(&Blob::new(&test_file), &test_file),
);
index.add(test_file.clone(), FileMetaData::new(&Blob::new(&test_file), &test_file));
}
let tree = super::Tree::new(&index);
@@ -223,10 +208,7 @@ mod test {
for test_file in test_files.clone() {
let test_file = PathBuf::from(test_file);
util::ensure_test_file(&test_file, None);
index.add(
test_file.clone(),
FileMetaData::new(&Blob::new(&test_file), &test_file),
);
index.add(test_file.clone(), FileMetaData::new(&Blob::new(&test_file), &test_file));
}
let tree = super::Tree::new(&index);
@@ -250,10 +232,7 @@ mod test {
util::ensure_test_file(&test_file, None);
let blob = Blob::new(&test_file);
test_blobs.push(blob.clone());
index.add(
test_file.clone(),
FileMetaData::new(&Blob::new(&test_file), &test_file),
);
index.add(test_file.clone(), FileMetaData::new(&Blob::new(&test_file), &test_file));
}
let tree = super::Tree::new(&index);

20
src/rustfmt.toml Normal file
View File

@@ -0,0 +1,20 @@
# Run rustfmt with this config (it should be picked up automatically).
version = "Two"
use_small_heuristics = "Max"
max_width = 100
struct_lit_width = 60
chain_width = 60
single_line_if_else_max_width = 60
single_line_let_else_max_width = 60
merge_derives = true
reorder_imports = true
# 格式化注释代码块Unstable
format_code_in_doc_comments = true
# 彩色输出Unstable
color = "Auto"
ignore = [
"tests",
]

View File

@@ -1 +1 @@
pub mod util;
pub mod util;

View File

@@ -1,7 +1,7 @@
use sha1::{Sha1, Digest};
use std::fs::File;
use std::io::{Write, BufReader, BufRead, Error};
use mit::utils::util;
use sha1::{Digest, Sha1};
use std::fs::File;
use std::io::{BufRead, BufReader, Error, Write};
#[test]
fn test_hash() {
@@ -42,4 +42,4 @@ fn test_string() {
s += "2";
s.push('!');
println!("{}", s);
}
}