update date to all parent
This commit is contained in:
parent
4ec389b6cc
commit
5ccce48381
@ -1,8 +1,9 @@
|
|||||||
use std::io::{self, Write};
|
use std::io::{self, Write};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::fs::{OpenOptions, self};
|
use std::fs::{self, OpenOptions};
|
||||||
use crypto::sha1::Sha1;
|
use crypto::sha1::Sha1;
|
||||||
use crypto::digest::Digest;
|
use crypto::digest::Digest;
|
||||||
|
use std::io::{Seek, SeekFrom, Read};
|
||||||
use crate::utils::{read, path};
|
use crate::utils::{read, path};
|
||||||
|
|
||||||
pub mod tree;
|
pub mod tree;
|
||||||
@ -13,7 +14,7 @@ pub mod blob;
|
|||||||
/// # Examples
|
/// # Examples
|
||||||
/// Input: /foo/bar
|
/// Input: /foo/bar
|
||||||
/// Result: ("tree hash(/foo/bar) bar", hash(/foo/bar), bar)
|
/// Result: ("tree hash(/foo/bar) bar", hash(/foo/bar), bar)
|
||||||
pub fn parse_path(path: &Path, is_blob: bool) -> (String, String, String) {
|
pub fn parse_path(path: PathBuf, is_blob: bool) -> (String, String, String) {
|
||||||
let file_name = path.file_name().unwrap().to_str().unwrap();
|
let file_name = path.file_name().unwrap().to_str().unwrap();
|
||||||
|
|
||||||
let mut hasher = Sha1::new();
|
let mut hasher = Sha1::new();
|
||||||
@ -86,6 +87,46 @@ fn add_node(path: &Path, node: &str) -> io::Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn update_dates(mut path: PathBuf, date: &str) {
|
||||||
|
let mut obj_p = path::objects();
|
||||||
|
|
||||||
|
while path.pop() {
|
||||||
|
let (dir, res) = hash_obj(path.to_str().unwrap());
|
||||||
|
obj_p.push(dir);
|
||||||
|
obj_p.push(res);
|
||||||
|
update_date(obj_p.clone(), date.clone());
|
||||||
|
obj_p.pop();
|
||||||
|
obj_p.pop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update_date(path: PathBuf, date: &str) -> io::Result<()> {
|
||||||
|
let mut file = OpenOptions::new()
|
||||||
|
.read(true)
|
||||||
|
.write(true)
|
||||||
|
.open(path.clone())?;
|
||||||
|
|
||||||
|
let mut buffer = [0; 1];
|
||||||
|
file.seek(SeekFrom::Start(0))?;
|
||||||
|
|
||||||
|
// Seek and read until a space is found
|
||||||
|
loop {
|
||||||
|
let bytes_read = file.read(&mut buffer)?;
|
||||||
|
if bytes_read == 0 {
|
||||||
|
// Reached the end of the file without finding a space
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if buffer[0] == b' ' {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
file.write_all(&date.as_bytes())?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
fn create_obj(name: String, content: &str) -> io::Result<()> {
|
fn create_obj(name: String, content: &str) -> io::Result<()> {
|
||||||
let mut root = path::objects();
|
let mut root = path::objects();
|
||||||
|
|
||||||
@ -110,6 +151,7 @@ pub fn get_timestamp(path_s: String) -> Option<i64> {
|
|||||||
let mut obj_p = path::objects();
|
let mut obj_p = path::objects();
|
||||||
|
|
||||||
let (dir, res) = hash_obj(&path_s);
|
let (dir, res) = hash_obj(&path_s);
|
||||||
|
dbg!((dir.clone(), res.clone()));
|
||||||
obj_p.push(dir);
|
obj_p.push(dir);
|
||||||
obj_p.push(res);
|
obj_p.push(res);
|
||||||
|
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
use std::io::{self};
|
use std::io;
|
||||||
use std::path::Path;
|
use std::path::PathBuf;
|
||||||
use std::fs::{self};
|
use std::fs;
|
||||||
use crate::utils::path;
|
use crate::utils::path;
|
||||||
use crate::store::head;
|
use crate::store::head;
|
||||||
use crate::store::object::{parse_path, add_node, create_obj, rm_node};
|
use crate::store::object::{update_dates, parse_path, add_node, create_obj, rm_node};
|
||||||
|
|
||||||
pub fn add(path: &Path, date: &str) -> io::Result<()> {
|
pub fn add(path: PathBuf, date: &str) -> io::Result<()> {
|
||||||
let (line, hash, name) = parse_path(path.clone(), true);
|
let (line, hash, name) = parse_path(path.clone(), true);
|
||||||
// add blob reference to parent
|
// add blob reference to parent
|
||||||
if path.iter().count() == 1 {
|
if path.iter().count() == 1 {
|
||||||
@ -21,10 +21,13 @@ pub fn add(path: &Path, date: &str) -> io::Result<()> {
|
|||||||
// create blob object
|
// create blob object
|
||||||
create_obj(hash, &content)?;
|
create_obj(hash, &content)?;
|
||||||
|
|
||||||
|
// update date for all parent
|
||||||
|
update_dates(path, date);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn rm(path: &Path) -> io::Result<()> {
|
pub fn rm(path: PathBuf) -> io::Result<()> {
|
||||||
let (line, hash, _) = parse_path(path.clone(), true);
|
let (line, hash, _) = parse_path(path.clone(), true);
|
||||||
|
|
||||||
// remove blob reference to parent
|
// remove blob reference to parent
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{self};
|
use std::io;
|
||||||
use std::path::Path;
|
use std::path::PathBuf;
|
||||||
use crate::utils::{read, path};
|
use crate::utils::{read, path};
|
||||||
use crate::store::head;
|
use crate::store::head;
|
||||||
use crate::store::object::{self, parse_path, hash_obj, add_node, create_obj};
|
use crate::store::object::{self, update_dates, parse_path, hash_obj, add_node, create_obj};
|
||||||
|
|
||||||
pub fn add(path: &Path, date: &str) -> io::Result<()> {
|
pub fn add(path: PathBuf, date: &str) -> io::Result<()> {
|
||||||
let (line, hash, name) = parse_path(path.clone(), false);
|
let (line, hash, name) = parse_path(path.clone(), false);
|
||||||
|
|
||||||
// add tree reference to parent
|
// add tree reference to parent
|
||||||
@ -21,15 +21,18 @@ pub fn add(path: &Path, date: &str) -> io::Result<()> {
|
|||||||
content.push_str(date);
|
content.push_str(date);
|
||||||
create_obj(hash, &content)?;
|
create_obj(hash, &content)?;
|
||||||
|
|
||||||
|
// update date for all parent
|
||||||
|
update_dates(path, date);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn rm(path: &Path) -> io::Result<()> {
|
pub fn rm(path: PathBuf) -> io::Result<()> {
|
||||||
let (_, lines) = read(path.to_path_buf().to_str().unwrap().to_string()).unwrap();
|
let (_, lines) = read(path.to_path_buf().to_str().unwrap().to_string()).unwrap();
|
||||||
for line in lines {
|
for line in lines {
|
||||||
let (ftype, hash, _) = parse_line(line.unwrap());
|
let (ftype, hash, _) = parse_line(line.unwrap());
|
||||||
if ftype == String::from("blob") {
|
if ftype == String::from("blob") {
|
||||||
object::rm(&hash);
|
object::rm(&hash)?;
|
||||||
} else {
|
} else {
|
||||||
rm_hash(hash);
|
rm_hash(hash);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user