add todos and fix some bugs on add and status

This commit is contained in:
grimhilt 2023-10-28 00:15:47 +02:00
parent fd477a8139
commit 56234eaa3d
3 changed files with 49 additions and 6 deletions

View File

@ -62,6 +62,7 @@ pub fn add(args: AddArgs) {
if Object::new(path.to_str().unwrap()).exists() { if Object::new(path.to_str().unwrap()).exists() {
added_files.push(String::from(f)); added_files.push(String::from(f));
} else { } else {
// todo applies regex
eprintln!("err: {} is not something you can add.", path.to_str().unwrap()); eprintln!("err: {} is not something you can add.", path.to_str().unwrap());
} }
} }

View File

@ -12,6 +12,10 @@ use crate::utils::read::{read_folder, read_lines};
use crate::store::object::tree; use crate::store::object::tree;
use crate::store::index; use crate::store::index;
pub struct StatusArgs {
pub nostyle: bool,
}
#[derive(PartialEq)] #[derive(PartialEq)]
enum RemoveSide { enum RemoveSide {
Left, Left,
@ -31,7 +35,7 @@ pub enum State {
// todo: relative path, filename // todo: relative path, filename
// todo: not catch added empty folder // todo: not catch added empty folder
pub fn status() { pub fn status(args: StatusArgs) {
let mut all_hashes = get_all_objs_hashes(); let mut all_hashes = get_all_objs_hashes();
let staged_objs = get_staged(&mut all_hashes); let staged_objs = get_staged(&mut all_hashes);
@ -39,9 +43,15 @@ pub fn status() {
x.1.clone() x.1.clone()
}).collect(); }).collect();
if args.nostyle
{
print_status_nostyle(staged_objs, objs);
}
else
{
print_status(staged_objs, objs); print_status(staged_objs, objs);
} }
}
pub fn get_all_objs() -> Vec<LocalObj> { pub fn get_all_objs() -> Vec<LocalObj> {
let all_hashes = get_all_objs_hashes(); let all_hashes = get_all_objs_hashes();
@ -82,6 +92,11 @@ fn get_all_objs_hashes() -> HashMap<String, LocalObj> {
fn should_retain(hasher: &mut Sha1, key: String, obj: LocalObj, move_copy_hashes: &mut HashMap<String, LocalObj>, del_objs_h: &mut HashMap<String, LocalObj>) -> bool { fn should_retain(hasher: &mut Sha1, key: String, obj: LocalObj, move_copy_hashes: &mut HashMap<String, LocalObj>, del_objs_h: &mut HashMap<String, LocalObj>) -> bool {
// todo prevent copied or moved if file empty // todo prevent copied or moved if file empty
// todo deal with directories
if obj.path.is_dir()
{
return false;
}
let mut blob = Blob::new(obj.path.clone()); let mut blob = Blob::new(obj.path.clone());
let mut flag = true; let mut flag = true;
let identical_blobs = blob.get_all_identical_blobs(); let identical_blobs = blob.get_all_identical_blobs();
@ -348,6 +363,26 @@ fn print_status(staged_objs: Vec<LocalObj>, objs: Vec<LocalObj>) {
} }
} }
fn print_status_nostyle(staged_objs: Vec<LocalObj>, objs: Vec<LocalObj>) {
// todo sort
if staged_objs.len() == 0 && objs.len() == 0 {
return;
}
for obj in staged_objs {
if obj.state == State::Deleted {
println!("deleted: {}", obj.name);
} else if obj.state == State::New {
println!("new: {}", obj.name);
} else if obj.state == State::Modified {
println!("modified: {}", obj.name);
} else if obj.state == State::Moved {
println!("moved: {} => {}", path_buf_to_string(obj.path_from.unwrap()), path_buf_to_string(obj.path));
} else if obj.state == State::Copied {
println!("copied: {} => {}", path_buf_to_string(obj.path_from.unwrap()), path_buf_to_string(obj.path));
}
}
}
fn print_object(obj: LocalObj) { fn print_object(obj: LocalObj) {
if obj.state == State::Deleted { if obj.state == State::Deleted {
println!(" {} {}", String::from("deleted:").red(), obj.name.red()); println!(" {} {}", String::from("deleted:").red(), obj.name.red());

View File

@ -2,6 +2,7 @@ use clap::{App, Arg, SubCommand};
use textwrap::{fill, Options}; use textwrap::{fill, Options};
use crate::commands::add::AddArgs; use crate::commands::add::AddArgs;
use crate::commands::status::StatusArgs;
use crate::commands::clone::{self, CloneArgs}; use crate::commands::clone::{self, CloneArgs};
mod commands; mod commands;
@ -66,6 +67,11 @@ fn main() {
.takes_value(true) .takes_value(true)
.value_name("DIRECTORY") .value_name("DIRECTORY")
) )
.arg(
Arg::with_name("nostyle")
.long("nostyle")
.help("Status with minium information and style"),
)
.about("Show the working tree status") .about("Show the working tree status")
) )
.subcommand( .subcommand(
@ -141,7 +147,9 @@ fn main() {
if let Some(val) = matches.values_of("directory") { if let Some(val) = matches.values_of("directory") {
global::global::set_dir_path(String::from(val.clone().next().unwrap())); global::global::set_dir_path(String::from(val.clone().next().unwrap()));
} }
commands::status::status(); commands::status::status(StatusArgs {
nostyle: matches.is_present("nostyle"),
});
} else if let Some(matches) = matches.subcommand_matches("add") { } else if let Some(matches) = matches.subcommand_matches("add") {
if let Some(files) = matches.values_of("files") { if let Some(files) = matches.values_of("files") {
commands::add::add(AddArgs { commands::add::add(AddArgs {
@ -191,7 +199,6 @@ fn main() {
} }
commands::pull::pull(); commands::pull::pull();
} else if let Some(_) = matches.subcommand_matches("test") { } else if let Some(_) = matches.subcommand_matches("test") {
} }
} }