Skip to content

Commit

Permalink
fix git repo deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerboa-app committed May 30, 2024
1 parent 11753b7 commit dd79b70
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 52 deletions.
29 changes: 0 additions & 29 deletions src/filesystem/folder.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,7 @@
use core::fmt;
use std::fs::DirEntry;

use regex::Regex;

#[derive(Debug, Clone)]
pub struct ListDirError
{
pub why: String
}

impl fmt::Display for ListDirError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.why)
}
}

/// List all files in path
pub fn list_dir(path: String) -> Result<std::fs::ReadDir, ListDirError>
{
match std::fs::read_dir(path)
{
Ok(files) =>
{
Ok(files)
},
Err(why) =>
{
Err(ListDirError { why: format!("{}", why)})
}
}
}

/// Return parsed [std::ffi::OsString] from [DirEntry]
pub fn dir_entry_to_path(d: DirEntry) -> Option<String>
{
Expand Down
25 changes: 24 additions & 1 deletion src/filesystem/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,25 @@
pub mod file;
pub mod folder;
pub mod folder;

/// Set all entries in a dir to permissions().set_readonly(readonly)
pub fn set_dir_readonly(dir: &str, readonly: bool) -> Result<(), std::io::Error>
{
let paths = std::fs::read_dir(dir.to_owned())?;

for path in paths
{
if path.is_err()
{
return Err(path.err().unwrap())
}

let path = path.unwrap();

match path.metadata()
{
Ok(p) => p.permissions().set_readonly(readonly),
Err(e) => return Err(e)
}
}
Ok(())
}
48 changes: 26 additions & 22 deletions src/integrations/git/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::path::Path;

use git2::{Cred, RemoteCallbacks, Repository};

use crate::config::GitConfig;
use crate::{config::GitConfig, filesystem::{folder::list_sub_dirs, set_dir_readonly}};

pub mod refresh;

Expand All @@ -30,6 +30,17 @@ impl From<git2::Error> for GitError
}
}

impl From<std::io::Error> for GitError
{
fn from(value: std::io::Error) -> Self
{
GitError
{
why: format!("std::io::Error {}", value)
}
}
}

/// Attempt to clone a remote repo from a [crate::config::GitConfig]
pub fn from_clone(path: &str, config: &GitConfig) -> Result<Repository, GitError>
{
Expand Down Expand Up @@ -95,31 +106,24 @@ pub fn from_clone(path: &str, config: &GitConfig) -> Result<Repository, GitError
}
}

pub fn remove_repository(dir: &str) -> Result<(), std::io::Error>
{
for dir in list_sub_dirs(dir.to_owned())
{
set_dir_readonly(&dir, false)?;
}
set_dir_readonly(dir, false)?;

std::fs::remove_dir_all(dir)?;

Ok(())
}

/// Make a fresh clone if [crate::config::Config::git] is present
/// deleting any file/dir called [crate::config::ContentConfig::path]
pub fn clean_and_clone(dir: &str, config: GitConfig) -> Result<Repository, GitError>
{
let path = Path::new(dir);
let result = if path.is_file()
{
std::fs::remove_file(path)
}
else if path.is_dir()
{
std::fs::remove_dir_all(path)
}
else
{
Ok(())
};
match result
{
Ok(_) => (),
Err(e) =>
{
return Err(GitError{why: format!("Could not clone, could not remove, {}", e)})
}
}
remove_repository(dir)?;
match from_clone(dir, &config)
{
Ok(repo) =>
Expand Down

0 comments on commit dd79b70

Please sign in to comment.