From e8c8ab9dfea9ab9b6de306eccac663d88aef681a Mon Sep 17 00:00:00 2001 From: grimhilt Date: Tue, 16 Apr 2024 17:54:25 +0200 Subject: [PATCH] fix(add): add deleted file --- src/commands/add.rs | 2 +- src/commands/init.rs | 18 +++++++++--------- src/store/object/object.rs | 12 +++++++++--- src/subcommands/add.rs | 10 ++++++---- src/subcommands/remote.rs | 5 +++-- src/utils/into.rs | 6 ++++++ 6 files changed, 34 insertions(+), 19 deletions(-) diff --git a/src/commands/add.rs b/src/commands/add.rs index 7b28c9a..1003da1 100644 --- a/src/commands/add.rs +++ b/src/commands/add.rs @@ -45,7 +45,7 @@ pub fn add(args: AddArgs) { add_entry(path, args.force, &mut added_files, rules.clone(), &mut ignored_f); }, false => { - if Object::new(path.to_str().unwrap()).exists() { + if Obj::from_path(file.clone()).exists_on_remote() { // object is deleted so not present but can still be added for deletion added_files.push(String::from(f)); } else { diff --git a/src/commands/init.rs b/src/commands/init.rs index 86e266f..d3f15b2 100644 --- a/src/commands/init.rs +++ b/src/commands/init.rs @@ -28,7 +28,7 @@ pub fn init() { path.push(".nextsync"); match builder.create(path.clone()) { Ok(()) => (), - Err(_) => println!("Error: cannot create .nextsync"), + Err(err) => println!("Error: cannot create .nextsync ({})", err), }; path.push("objects"); @@ -59,12 +59,12 @@ pub fn init() { } // todo - path.pop(); - path.pop(); - path.push(".nextsyncignore"); - - match File::create(path) { - Ok(_) => (), - Err(_) => println!("Error: cannot create .nextsyncignore"), - } + // path.pop(); + // path.pop(); + // path.push(".nextsyncignore"); + // + // match File::create(path) { + // Ok(_) => (), + // Err(_) => println!("Error: cannot create .nextsyncignore"), + // } } diff --git a/src/store/object/object.rs b/src/store/object/object.rs index e2c0012..40c5466 100644 --- a/src/store/object/object.rs +++ b/src/store/object/object.rs @@ -129,7 +129,7 @@ impl ObjMethods for Obj { } fn exists_on_remote(&mut self) -> bool { - PathBuf::from(self.get_hash_path()).exists() + self.obj_path.exists() } } @@ -287,6 +287,7 @@ impl Obj { } pub fn from_path(path: S) -> Obj where S: IntoPathBuf { + let path = path.into(); let mut hasher = Sha1::new(); hasher.input_str(path.to_str().unwrap()); @@ -297,8 +298,13 @@ impl Obj { obj_path.push(dir); obj_path.push(res); + // set to absolute path if not already let root = path::repo_root(); - let abs_path = root.join(path.clone()); + let abs_path = match path.clone().starts_with(root.clone()) { + true => path.clone(), + false => root.join(path.clone()) + }; + Obj { name: match abs_path.file_name() { None => String::new(), @@ -321,7 +327,7 @@ impl Obj { /// load from the information line stored in the object pub fn from_line(line: String, base_dir: Option) -> Box { - let mut split = line.rsplit(' '); + let mut split = line.trim().rsplit(' '); if split.clone().count() != 3 { eprintln!("fatal: invalid object(s)"); std::process::exit(1); diff --git a/src/subcommands/add.rs b/src/subcommands/add.rs index cdfa1a1..25ff2a6 100644 --- a/src/subcommands/add.rs +++ b/src/subcommands/add.rs @@ -1,4 +1,4 @@ -use clap::{Arg, ArgMatches, Command}; +use clap::{Arg, ArgMatches, Command, ArgAction}; use crate::commands; use crate::commands::add::AddArgs; @@ -7,7 +7,7 @@ pub fn create() -> Command { Command::new("add") .arg( Arg::new("files") - .required(true) + .required_unless_present("all") .conflicts_with("all") .num_args(1..) .value_name("FILE") @@ -17,12 +17,14 @@ pub fn create() -> Command { Arg::new("force") .short('f') .long("force") + .action(ArgAction::SetTrue) .help("Allow adding otherwise ignored files."), ) .arg( Arg::new("all") .short('A') .long("all") + .action(ArgAction::SetTrue) .help("This adds, modifies, and removes index entries to match the working tree"), ) .about("Add changes to the index") @@ -34,7 +36,7 @@ pub fn handler(args: &ArgMatches) { None => vec![], Some(vals) => vals.map(|s| s.to_string()).collect(), }, - force: args.contains_id("force"), - all: args.contains_id("all"), + force: *args.get_one::("force").unwrap(), + all: *args.get_one::("all").unwrap(), }); } diff --git a/src/subcommands/remote.rs b/src/subcommands/remote.rs index 17ff076..164511e 100644 --- a/src/subcommands/remote.rs +++ b/src/subcommands/remote.rs @@ -1,4 +1,4 @@ -use clap::{Arg, Command, ArgMatches}; +use clap::{Arg, Command, ArgMatches, ArgAction}; use crate::commands; use crate::commands::remote::RemoteArgs; @@ -26,6 +26,7 @@ pub fn create() -> Command { Arg::new("verbose") .short('v') .long("verbose") + .action(ArgAction::SetTrue) .help("Be a little more verbose and show remote url after name.") ) } @@ -39,7 +40,7 @@ pub fn handler(args: &ArgMatches) { }); } _ => { - commands::remote::remote_list(args.contains_id("verbose")); + commands::remote::remote_list(*args.get_one::("verbose").unwrap()); } } } diff --git a/src/utils/into.rs b/src/utils/into.rs index 535fd4b..fc5f0df 100644 --- a/src/utils/into.rs +++ b/src/utils/into.rs @@ -22,3 +22,9 @@ impl IntoPathBuf for String { } } +impl IntoPathBuf for &str { + fn into(self) -> PathBuf { + PathBuf::from(self) + } +} +