From eedb003d8c001f76ab46e41558cb8c1d706ac463 Mon Sep 17 00:00:00 2001 From: grimhilt Date: Sat, 3 Jun 2023 17:29:41 +0200 Subject: [PATCH] add staged in status --- src/commands/add.rs | 11 ++---- src/commands/status.rs | 82 +++++++++++++++++++++--------------------- src/utils.rs | 2 ++ src/utils/index.rs | 27 ++++++++++++++ src/utils/read.rs | 20 +++++++++++ 5 files changed, 93 insertions(+), 49 deletions(-) create mode 100644 src/utils/index.rs create mode 100644 src/utils/read.rs diff --git a/src/commands/add.rs b/src/commands/add.rs index 159492b..7d493af 100644 --- a/src/commands/add.rs +++ b/src/commands/add.rs @@ -1,8 +1,7 @@ use clap::Values; -use std::fs::OpenOptions; use crate::utils; use std::path::Path; -use std::io::Write +use std::io::Write; pub fn add(files: Values<'_>) { let root = match utils::path::nextsync_root() { @@ -15,13 +14,7 @@ pub fn add(files: Values<'_>) { let mut index_path = root.clone(); index_path.push(".nextsync"); - index_path.push("index"); - let mut index_file = OpenOptions::new() - .read(true) - .write(true) - .append(true) - .create(true) - .open(index_path).expect("Cannot open index file"); + let mut index_file = utils::index::open(index_path); let file_vec: Vec<&str> = files.collect(); for file in file_vec { diff --git a/src/commands/status.rs b/src/commands/status.rs index e28d007..247f8c7 100644 --- a/src/commands/status.rs +++ b/src/commands/status.rs @@ -1,21 +1,27 @@ -use std::env; -use std::path::Path; +use crate::utils; use std::fs::File; -use std::io::{self, BufRead}; -use std::path::PathBuf; use crypto::digest::Digest; use crypto::sha1::Sha1; use std::collections::HashSet; use colored::Colorize; -use std::fs; +use std::path::PathBuf; +use std::io; // todo: relative path, filename, get modified pub fn status() { let mut hashes = HashSet::new(); let mut objects: Vec = vec![]; + let mut staged_objects: Vec = vec![]; - let path = env::current_dir().unwrap(); - let mut next_sync_path = path.clone(); + let root = match utils::path::nextsync_root() { + Some(path) => path, + None => { + eprintln!("fatal: not a nextsync repository (or any of the parent directories): .nextsync"); + std::process::exit(1); + } + }; + + let mut next_sync_path = root.clone(); next_sync_path.push(".nextsync"); @@ -30,35 +36,48 @@ pub fn status() { } } - if let Ok(entries) = read_folder(path.clone()) { + if let Ok(entries) = utils::read::read_folder(root.clone()) { for entry in entries { if !is_nextsync_config(entry.clone()) { - let object_path = entry.strip_prefix(path.clone()).unwrap(); + let object_path = entry.strip_prefix(root.clone()).unwrap(); objects.push(String::from(object_path.to_str().unwrap())); } } } find_missing_elements(&mut hashes, &mut objects); - print_status(hashes.clone(), objects.clone()); + if let Ok(entries) = utils::index::read_line(next_sync_path.clone()) { + for entry in entries { + staged_objects.push(String::from(entry.unwrap())); + } + } + print_status(staged_objects.clone(), hashes.clone(), objects.clone()); dbg!(hashes); dbg!(objects); } -fn print_status(hashes: HashSet, objects: Vec) { - if objects.len() == 0 { - println!("No new file"); - } else { - for object in objects { +fn print_status(staged_objects: Vec, hashes: HashSet, objects: Vec) { + if staged_objects.len() == 0 { + println!("No staged file"); + } else { + println!("Staged files: "); + for staged in staged_objects { + println!("{} {}", String::from("Staged:").green(), staged.green()); + } + } + if objects.len() == 0 { + println!("No new file"); + } else { + for object in objects { println!("{} {}", String::from("Added:").green(), object.green()); - } - } + } + } - if hashes.len() != 0 { - for hash in hashes { - println!("{} {}", String::from("Deleted:").red(), hash.red()); - } - } + if hashes.len() != 0 { + for hash in hashes { + println!("{} {}", String::from("Deleted:").red(), hash.red()); + } + } } fn find_missing_elements(hashes: &mut HashSet, objects: &mut Vec) { @@ -94,24 +113,7 @@ fn is_nextsync_config(path: PathBuf) -> bool { fn read_head(mut path: PathBuf) -> io::Result>> { path.push("HEAD"); - read_lines(path) -} - -fn read_folder(path: PathBuf) -> io::Result> { - let mut entries = fs::read_dir(path)? - .map(|res| res.map(|e| e.path())) - .collect::, io::Error>>()?; - - entries.sort(); - Ok(entries) -} - -// The output is wrapped in a Result to allow matching on errors -// Returns an Iterator to the Reader of the lines of the file. -fn read_lines

(filename: P) -> io::Result>> -where P: AsRef, { - let file = File::open(filename)?; - Ok(io::BufReader::new(file).lines()) + utils::read::read_lines(path) } #[cfg(test)] diff --git a/src/utils.rs b/src/utils.rs index 4da9789..305d212 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1 +1,3 @@ pub mod path; +pub mod index; +pub mod read; diff --git a/src/utils/index.rs b/src/utils/index.rs new file mode 100644 index 0000000..3d60681 --- /dev/null +++ b/src/utils/index.rs @@ -0,0 +1,27 @@ +use std::fs::OpenOptions; +use std::fs::File; +use std::path::PathBuf; +use crate::utils::read; +use std::io; + +pub fn read_only(mut path: PathBuf) -> File { + path.push("index"); + OpenOptions::new() + .read(true) + .open(path).expect("Cannot open index file") +} + +pub fn open(mut path: PathBuf) -> File { + path.push("index"); + OpenOptions::new() + .read(true) + .write(true) + .append(true) + .create(true) + .open(path).expect("Cannot open index file") +} + +pub fn read_line(mut path: PathBuf) -> io::Result>> { + path.push("index"); + read::read_lines(path) +} diff --git a/src/utils/read.rs b/src/utils/read.rs new file mode 100644 index 0000000..63ff5b8 --- /dev/null +++ b/src/utils/read.rs @@ -0,0 +1,20 @@ +use std::path::{Path, PathBuf}; +use std::io::{self, BufRead}; +use std::fs::{self, File}; + +// The output is wrapped in a Result to allow matching on errors +// Returns an Iterator to the Reader of the lines of the file. +pub fn read_lines

(filename: P) -> io::Result>> +where P: AsRef, { + let file = File::open(filename)?; + Ok(io::BufReader::new(file).lines()) +} + +pub fn read_folder(path: PathBuf) -> io::Result> { + let mut entries = fs::read_dir(path)? + .map(|res| res.map(|e| e.path())) + .collect::, io::Error>>()?; + + entries.sort(); + Ok(entries) +}