diff --git a/src/base.rs b/src/base.rs index e69de29..f00e5aa 100644 --- a/src/base.rs +++ b/src/base.rs @@ -0,0 +1,17 @@ +use std::fs; + +pub fn write_tree(directory: String) { + let entries = fs::read_dir(&directory).unwrap(); + + for entry in entries { + let item = entry.unwrap(); + let metadata = item.metadata().unwrap(); + let name = item.file_name(); + let full = format!("{}/{}", directory, name.to_str().unwrap()); + if metadata.is_file() { + println!("{}", full); + } else if metadata.is_dir() { + write_tree(full); + } + } +} diff --git a/src/main.rs b/src/main.rs index 00db0b6..e3798a5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,12 +19,17 @@ fn main() { .about("outputs the original object from the provided hash") .arg(Arg::with_name("hash").index(1).required(true)), ) + .subcommand( + SubCommand::with_name("write-tree") + .about("write the current working directory to the database"), + ) .get_matches(); match matches.subcommand_name() { Some("init") => init(), Some("hash-object") => hash_object(matches), Some("cat-file") => cat_file(matches), + Some("write-tree") => write_tree(), _ => println!("unknown sub command"), } } @@ -54,3 +59,7 @@ fn cat_file(matches: ArgMatches) { println!("{}", file_contents) } } + +fn write_tree() { + base::write_tree(".".to_owned()) +}