add staged in status

This commit is contained in:
grimhilt 2023-06-03 17:29:41 +02:00
parent 48286c69b4
commit eedb003d8c
5 changed files with 93 additions and 49 deletions

View File

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

View File

@ -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<String> = vec![];
let mut staged_objects: Vec<String> = 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,22 +36,35 @@ 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<String>, objects: Vec<String>) {
fn print_status(staged_objects: Vec<String>, hashes: HashSet<String>, objects: Vec<String>) {
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 {
@ -94,24 +113,7 @@ fn is_nextsync_config(path: PathBuf) -> bool {
fn read_head(mut path: PathBuf) -> io::Result<io::Lines<io::BufReader<File>>> {
path.push("HEAD");
read_lines(path)
}
fn read_folder(path: PathBuf) -> io::Result<Vec<PathBuf>> {
let mut entries = fs::read_dir(path)?
.map(|res| res.map(|e| e.path()))
.collect::<Result<Vec<_>, 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<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where P: AsRef<Path>, {
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
utils::read::read_lines(path)
}
#[cfg(test)]

View File

@ -1 +1,3 @@
pub mod path;
pub mod index;
pub mod read;

27
src/utils/index.rs Normal file
View File

@ -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<io::Lines<io::BufReader<File>>> {
path.push("index");
read::read_lines(path)
}

20
src/utils/read.rs Normal file
View File

@ -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<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where P: AsRef<Path>, {
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}
pub fn read_folder(path: PathBuf) -> io::Result<Vec<PathBuf>> {
let mut entries = fs::read_dir(path)?
.map(|res| res.map(|e| e.path()))
.collect::<Result<Vec<_>, io::Error>>()?;
entries.sort();
Ok(entries)
}