rgit/src/main.rs

66 lines
2.1 KiB
Rust
Raw Normal View History

2020-09-29 01:04:28 +02:00
use clap::{App, Arg, ArgMatches, SubCommand};
use std::fs;
2020-10-05 14:02:20 +02:00
mod base;
2020-09-27 14:52:54 +02:00
mod data;
2020-09-27 01:25:10 +02:00
2020-09-25 18:51:39 +02:00
fn main() {
2020-09-27 01:25:10 +02:00
let matches = App::new("rgit vcs")
.version("0.1.0")
.author("Gonçalo Valério <gon@ovalerio.net>")
.about("A watered-down git clone")
.subcommand(SubCommand::with_name("init").about("creates new repository"))
2020-09-29 01:04:28 +02:00
.subcommand(
SubCommand::with_name("hash-object")
.about("created an hash for an object")
.arg(Arg::with_name("file").index(1).required(true)),
)
2020-09-29 12:08:12 +02:00
.subcommand(
SubCommand::with_name("cat-file")
.about("outputs the original object from the provided hash")
.arg(Arg::with_name("hash").index(1).required(true)),
)
2020-10-05 19:49:44 +02:00
.subcommand(
SubCommand::with_name("write-tree")
.about("write the current working directory to the database"),
)
2020-09-27 01:25:10 +02:00
.get_matches();
2020-09-29 01:04:28 +02:00
match matches.subcommand_name() {
Some("init") => init(),
Some("hash-object") => hash_object(matches),
2020-09-29 12:08:12 +02:00
Some("cat-file") => cat_file(matches),
2020-10-05 19:49:44 +02:00
Some("write-tree") => write_tree(),
2020-09-29 01:04:28 +02:00
_ => println!("unknown sub command"),
2020-09-27 01:25:10 +02:00
}
}
fn init() {
2020-09-27 14:52:54 +02:00
match data::init() {
Ok(()) => println!("Repository created"),
_ => println!("Failed. Perhaps the repository already exists."),
}
2020-09-25 18:51:39 +02:00
}
2020-09-29 01:04:28 +02:00
fn hash_object(matches: ArgMatches) {
if let Some(cmd_matches) = matches.subcommand_matches("hash-object") {
let content = fs::read(cmd_matches.value_of("file").unwrap())
.expect("Something went wrong reading the provided file");
2020-10-05 13:58:48 +02:00
let hash = data::hash_object(&content, "blob".to_owned());
2020-09-29 01:04:28 +02:00
println!("{}", hash);
}
}
2020-09-29 12:08:12 +02:00
fn cat_file(matches: ArgMatches) {
if let Some(cmd_matches) = matches.subcommand_matches("cat-file") {
2020-10-05 13:58:48 +02:00
let file_contents = data::get_object(
cmd_matches.value_of("hash").unwrap().to_owned(),
"".to_owned(),
);
2020-09-29 12:08:12 +02:00
println!("{}", file_contents)
}
}
2020-10-05 19:49:44 +02:00
fn write_tree() {
base::write_tree(".".to_owned())
}