implement -all option to add

This commit is contained in:
grimhilt 2023-08-25 18:52:29 +02:00
parent 41c4796555
commit 57647e5df2
3 changed files with 47 additions and 6 deletions

View File

@ -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<Values<'a>>,
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<String> = 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<String>) {
}
}
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()));
}
}

View File

@ -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") {

View File

@ -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)