From f67fd2bd60d7aa299a7f1511dc61268edc5bdc83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A7alo=20Val=C3=A9rio?= Date: Mon, 7 Dec 2020 19:12:53 +0000 Subject: [PATCH] step sixty three: record added files in the index --- Cargo.lock | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 ++ src/base.rs | 12 ++++++++ src/data.rs | 34 +++++++++++++++++++++++ src/main.rs | 13 +++++++++ 5 files changed, 141 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 118f406..287a0c9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -101,6 +101,12 @@ dependencies = [ "libc", ] +[[package]] +name = "itoa" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6" + [[package]] name = "lazy_static" version = "1.4.0" @@ -125,6 +131,24 @@ version = "0.2.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" +[[package]] +name = "proc-macro2" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quote" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37" +dependencies = [ + "proc-macro2", +] + [[package]] name = "rand" version = "0.7.3" @@ -187,11 +211,19 @@ version = "0.1.0" dependencies = [ "clap", "lazy_static", + "serde", + "serde_json", "sha-1", "tempfile", "walkdir", ] +[[package]] +name = "ryu" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" + [[package]] name = "same-file" version = "1.0.6" @@ -201,6 +233,37 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "serde" +version = "1.0.118" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06c64263859d87aa2eb554587e2d23183398d617427327cf2b3d0ed8c69e4800" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.118" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c84d3526699cd55261af4b941e4e725444df67aa4f9e6a3564f18030d12672df" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.60" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1500e84d27fe482ed1dc791a56eddc2f230046a040fa908c08bda1d9fb615779" +dependencies = [ + "itoa", + "ryu", + "serde", +] + [[package]] name = "sha-1" version = "0.9.1" @@ -220,6 +283,17 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" +[[package]] +name = "syn" +version = "1.0.53" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8833e20724c24de12bbaba5ad230ea61c3eafb05b881c7c9d3cfe8638b187e68" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + [[package]] name = "tempfile" version = "3.1.0" @@ -255,6 +329,12 @@ version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" +[[package]] +name = "unicode-xid" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" + [[package]] name = "vec_map" version = "0.8.2" diff --git a/Cargo.toml b/Cargo.toml index 8dc4759..103bf8d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,3 +12,5 @@ sha-1 = "0.9.1" walkdir = "2" tempfile = "3.1.0" lazy_static = "1.4.0" +serde = {version = "1.0.118", features = ["derive"]} +serde_json = "1.0.60" diff --git a/src/base.rs b/src/base.rs index bd6ec1c..b119788 100644 --- a/src/base.rs +++ b/src/base.rs @@ -470,6 +470,18 @@ pub fn is_ancestor_of(commit: String, maybe_ancestor: String) -> bool { return false; } +pub fn add(files: Vec<&str>) { + let mut index = data::get_index(); + for file in files { + let content = fs::read(file).expect("Failed to read file"); + index.insert( + file.to_owned(), + data::hash_object(&content, "blob".to_owned()), + ); + } + data::set_index(index); +} + fn empty_current_directory(dir: &str) -> io::Result<()> { // Delete current directory, except the ignored directories and files for entry in fs::read_dir(dir)? { diff --git a/src/data.rs b/src/data.rs index 0e0f74a..23ff61e 100644 --- a/src/data.rs +++ b/src/data.rs @@ -1,5 +1,8 @@ use lazy_static::lazy_static; +use serde::{Deserialize, Serialize}; +use serde_json::Result; use sha1::{Digest, Sha1}; +use std::collections::HashMap; use std::fs; use std::path::Path; use std::str; @@ -18,6 +21,11 @@ pub struct RefValue { pub symbolic: bool, } +#[derive(Serialize, Deserialize)] +struct Index { + files: HashMap, +} + // The below two methods are not the same thing as a "context manager" // I might need to replace it later with a better alternative. pub fn set_rgit_dir(path: &str) { @@ -195,3 +203,29 @@ fn object_exists(oid: String) -> bool { let path = format!("{}/objects/{}", dir.clone(), oid.clone()); return Path::new(path.as_str()).exists(); } + +pub fn get_index() -> HashMap { + let mut index_files = HashMap::new(); + let dir = RGIT_DIR.lock().unwrap().to_owned(); + let index_path = format!("{}/index", dir.clone()); + let path = Path::new(index_path.as_str()); + + if path.exists() { + let index_content = fs::read_to_string(path).expect("Failed to read index file"); + let index: Index = serde_json::from_str(index_content.as_str()).unwrap(); + index_files = index.files; + } + + return index_files; +} + +pub fn set_index(files: HashMap) { + let new_index = Index { files: files }; + let index_content = serde_json::to_string(&new_index).expect("Failed to serialize index"); + + let dir = RGIT_DIR.lock().unwrap().to_owned(); + let index_path = format!("{}/index", dir.clone()); + let path = Path::new(index_path.as_str()); + + fs::write(path, index_content).expect("Failed to write index"); +} diff --git a/src/main.rs b/src/main.rs index 34c4c10..6dff8d3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -104,6 +104,11 @@ fn main() { .arg(Arg::with_name("remote").index(1).required(true)) .arg(Arg::with_name("branch").index(2).required(true)), ) + .subcommand( + SubCommand::with_name("add") + .about("Add files to the index") + .arg(Arg::with_name("files").multiple(true)), + ) .get_matches(); data::set_rgit_dir("."); @@ -127,6 +132,7 @@ fn main() { Some("merge-base") => merge_base(matches), Some("fetch") => fetch(matches), Some("push") => push(matches), + Some("add") => add(matches), _ => println!("unknown sub command"), } data::reset_rgit_dir(); @@ -372,6 +378,13 @@ fn push(matches: ArgMatches) { } } +fn add(matches: ArgMatches) { + if let Some(cmd_matches) = matches.subcommand_matches("add") { + let files: Vec<&str> = cmd_matches.values_of("files").unwrap().collect(); + base::add(files); + } +} + fn print_commit(oid: String, commit: &base::Commit, mut refs: HashMap>) { let ref_str = if refs.contains_key(&oid) { refs.get_mut(&oid).unwrap().join(", ")