feat(add): prevent adding a file without changes

This commit is contained in:
grimhilt 2024-05-07 18:12:05 +02:00
parent 939b6f2fe3
commit 980d2d9a5d
5 changed files with 54 additions and 10 deletions

View File

@ -42,7 +42,10 @@ pub fn add(args: AddArgs) {
let path = repo_root().join(Path::new(&f));
match path.exists() {
true => {
add_entry(path, args.force, &mut added_files, rules.clone(), &mut ignored_f);
let mut obj = Obj::from_path(f.clone());
if obj.has_changes() {
add_entry(path, args.force, &mut added_files, rules.clone(), &mut ignored_f);
}
},
false => {
if Obj::from_path(file.clone()).exists_on_remote() {

View File

@ -32,6 +32,7 @@ pub trait ObjMethods {
fn rm_node(&mut self) -> io::Result<()>;
fn rm_node_down(&mut self) -> io::Result<()>;
fn exists_on_remote(&mut self) -> bool;
fn has_changes(&mut self) -> bool;
}
pub struct Obj {
@ -133,6 +134,20 @@ impl ObjMethods for Obj {
fn exists_on_remote(&mut self) -> bool {
self.obj_path.exists()
}
fn has_changes(&mut self) -> bool {
if !self.obj_path.exists() {
return true;
}
match self.obj_type {
ObjType::BLOB => Blob::from_path(self.relative_file_path.clone()).has_changes(),
ObjType::TREE => Tree::from_path(self.relative_file_path.clone()).has_changes(),
ObjType::DEFAULT => {
unreachable!();
}
}
}
}
impl ObjMethods for Blob {
@ -200,6 +215,10 @@ impl ObjMethods for Blob {
fn exists_on_remote(&mut self) -> bool {
self.obj.exists_on_remote()
}
fn has_changes(&mut self) -> bool {
self.obj.has_changes()
}
}
impl ObjMethods for Tree {
@ -277,6 +296,10 @@ impl ObjMethods for Tree {
fn exists_on_remote(&mut self) -> bool {
self.obj.exists_on_remote()
}
fn has_changes(&mut self) -> bool {
self.obj.has_changes()
}
}
impl Obj {

View File

@ -11,6 +11,7 @@ pub struct Tree {
is_head: bool,
}
impl Tree {
pub fn new(obj: Obj) -> Self {
Tree {
@ -50,6 +51,11 @@ impl Tree {
}
}
pub fn has_changes(&mut self) -> bool {
todo!();
return true;
}
pub fn next(&mut self) -> Option<Box<dyn ObjMethods>> {
self.read();
//if let Some(ref mut file) = self.buf_reader {

View File

@ -40,6 +40,8 @@ fn collect_status_lines(client: &mut ClientTest) -> Vec<String> {
#[cfg(test)]
mod add_tests {
use crate::utils::{server::ServerTest, status_utils::status_should_be_empty};
use super::*;
#[test]
@ -95,14 +97,24 @@ mod add_tests {
}
#[test]
fn add_no_change() {
assert!(false);
fn add_file_no_changes() {
// add a file push it and add it again
// let id = get_random_test_id();
// let mut client = ClientTest::new(id).init();
//
//
// client.clean();
let id = get_random_test_id();
let mut server = ServerTest::new(id.clone());
server.init();
let mut client = ClientTest::new(id).init();
let _ = client.add_file("file1", "foo");
client.run_cmd_ok("add file1");
client.run_cmd_ok("push");
status_should_be_empty(&mut client);
client.run_cmd_ok("add file1");
status_should_be_empty(&mut client);
client.clean();
server.clean();
}
}

View File

@ -15,13 +15,13 @@ pub fn status_should_be_empty(client: &mut ClientTest) {
let (staged, not_staged) = client.get_status();
if staged.len() != 0 {
eprintln!("id: {}", client.test_id.clone());
eprintln!("Staged should be empty but has '{}'", staged.len());
eprintln!("Staged should be empty but has '{}' line(s)", staged.len());
assert!(staged.len() == 0);
}
if staged.len() != 0 {
eprintln!("id: {}", client.test_id.clone());
eprintln!("Not Staged should be empty but has '{}'", not_staged.len());
eprintln!("Not Staged should be empty but has '{}' line(s)", not_staged.len());
assert!(not_staged.len() == 0);
}
}