From 57647e5df2df9f9bded6e9f6b63e254597e8148a Mon Sep 17 00:00:00 2001 From: grimhilt Date: Fri, 25 Aug 2023 18:52:29 +0200 Subject: [PATCH] implement -all option to add --- src/commands/add.rs | 28 +++++++++++++++++++++++++--- src/main.rs | 16 +++++++++++++++- src/store/index.rs | 9 +++++++-- 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/src/commands/add.rs b/src/commands/add.rs index 922b810..11840e4 100644 --- a/src/commands/add.rs +++ b/src/commands/add.rs @@ -1,20 +1,31 @@ use std::io::Write; +use std::fs::OpenOptions; use std::path::{Path, PathBuf}; use clap::Values; +use crate::store::index; use crate::store::{self, object::Object}; use crate::utils; use crate::utils::nextsyncignore::{self, ignore_file}; -use crate::utils::path::{normalize_relative, repo_root}; +use crate::utils::path::{normalize_relative, repo_root, path_buf_to_string}; + +use super::status::get_all_objs; pub struct AddArgs<'a> { - pub files: Values<'a>, + pub files: Option>, pub force: bool, + pub all: bool, } // todo match deleted files // todo match weird reg expression // todo -A == . pub fn add(args: AddArgs) { + // write all modification in the index + if args.all { + write_all(); + return; + } + let mut index_file = store::index::open(); let mut added_files: Vec = vec![]; @@ -24,7 +35,7 @@ pub fn add(args: AddArgs) { }; let mut ignored_f = vec![]; - let file_vec: Vec<&str> = args.files.collect(); + let file_vec: Vec<&str> = args.files.unwrap().collect(); for file in file_vec { let f = match normalize_relative(file) { @@ -95,3 +106,14 @@ fn add_folder_content(path: PathBuf, added_files: &mut Vec) { } } + +fn write_all() { + let objs = get_all_objs(); + let mut index_file = OpenOptions::new() + .write(true) + .create(true) + .open(index::path()).expect("Cannot open index file"); + for obj in objs { + writeln!(index_file, "{}", path_buf_to_string(obj.path.clone())); + } +} diff --git a/src/main.rs b/src/main.rs index 736c4e7..acfa849 100644 --- a/src/main.rs +++ b/src/main.rs @@ -81,6 +81,7 @@ fn main() { .arg( Arg::with_name("files") .required(true) + .conflicts_with("all") .multiple(true) .takes_value(true) .value_name("FILE") @@ -92,6 +93,12 @@ fn main() { .long("force") .help("Allow adding otherwise ignored files."), ) + .arg( + Arg::with_name("all") + .short("A") + .long("all") + .help("This adds, modifies, and removes index entries to match the working tree"), + ) .about("Add changes to the index") ) .subcommand( @@ -138,8 +145,15 @@ fn main() { } else if let Some(matches) = matches.subcommand_matches("add") { if let Some(files) = matches.values_of("files") { commands::add::add(AddArgs { - files, + files: Some(files), force: matches.is_present("force"), + all: matches.is_present("all"), + }); + } else { + commands::add::add(AddArgs { + files: None, + force: matches.is_present("force"), + all: matches.is_present("all"), }); } } else if let Some(_) = matches.subcommand_matches("reset") { diff --git a/src/store/index.rs b/src/store/index.rs index 9e7f7f2..7ecca48 100644 --- a/src/store/index.rs +++ b/src/store/index.rs @@ -1,12 +1,17 @@ use std::io; +use std::path::PathBuf; use std::fs::File; use std::fs::OpenOptions; use crate::utils::{read, path}; -pub fn open() -> File { +pub fn path() -> PathBuf { let mut path = path::nextsync(); - path.push("index"); + path +} + +pub fn open() -> File { + let mut path = path(); OpenOptions::new() .read(true) .write(true)