diff --git a/.gitignore b/.gitignore index 196e176..bca3248 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,9 @@ Cargo.lock # Added by cargo /target + +## IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 84a0adb..aca5246 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,5 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +sha1 = "0.10.6" +hex = "0.4.3" \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..dde4911 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,3 @@ +// 不使用lib.rs的话,就无法在tests里引用到src中的模块 +pub mod objects; +pub mod utils; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index e7a11a9..89e2949 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +use std::collections::HashMap; +type Index = HashMap; fn main() { println!("Hello, world!"); } diff --git a/src/objects/blob.rs b/src/objects/blob.rs new file mode 100644 index 0000000..ed3dcaa --- /dev/null +++ b/src/objects/blob.rs @@ -0,0 +1,6 @@ +use crate::objects::object::Hash; +#[derive(Debug, Clone)] +pub struct Blob { + hash: Hash, + data: String, +} \ No newline at end of file diff --git a/src/objects/commit.rs b/src/objects/commit.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/objects/mod.rs b/src/objects/mod.rs new file mode 100644 index 0000000..e0c18af --- /dev/null +++ b/src/objects/mod.rs @@ -0,0 +1,4 @@ +pub mod commit; +pub mod blob; +pub mod tree; +mod object; \ No newline at end of file diff --git a/src/objects/object.rs b/src/objects/object.rs new file mode 100644 index 0000000..ff70033 --- /dev/null +++ b/src/objects/object.rs @@ -0,0 +1 @@ +pub type Hash = String; \ No newline at end of file diff --git a/src/objects/tree.rs b/src/objects/tree.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/utils/mod.rs b/src/utils/mod.rs new file mode 100644 index 0000000..929d03a --- /dev/null +++ b/src/utils/mod.rs @@ -0,0 +1 @@ +pub mod util; \ No newline at end of file diff --git a/src/utils/util.rs b/src/utils/util.rs new file mode 100644 index 0000000..c228431 --- /dev/null +++ b/src/utils/util.rs @@ -0,0 +1,11 @@ +use sha1::{ + Digest, + Sha1 +}; + +pub fn calc_hash(data: &String) -> String { + let mut hasher = Sha1::new(); + hasher.update(data); + let hash = hasher.finalize(); + hex::encode(hash) +} \ No newline at end of file diff --git a/tests/test.rs b/tests/test.rs new file mode 100644 index 0000000..e240b16 --- /dev/null +++ b/tests/test.rs @@ -0,0 +1,42 @@ +use sha1::{Sha1, Digest}; +use std::fs::File; +use std::io::{Write, BufReader, BufRead, Error}; + +#[test] +fn test_hash() { + let mut hasher = Sha1::new(); + hasher.update(String::from("hello world")); + let result = format!("{:x}", hasher.finalize()); + println!("{}", result); + println!("{}", mini_git::utils::util::calc_hash(&String::from("hello world"))); +} + +#[test] +fn test_write() -> Result<(), Error> { + let path = "lines.txt"; + //create会截断文件 + let mut output = File::create(path)?; // ? 用于传播错误 + write!(output, "Rust\nWrite\nRead4")?; + Ok(()) +} + +#[test] +fn test_read() -> Result<(), Error> { + let path = "lines.txt"; + 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); +} \ No newline at end of file