Skip to content

Commit

Permalink
fix(repository): when clone failed, remove the cloned directory
Browse files Browse the repository at this point in the history
  • Loading branch information
wsxiaoys committed Apr 28, 2024
1 parent fe3b270 commit 9822c2f
Showing 1 changed file with 33 additions and 26 deletions.
59 changes: 33 additions & 26 deletions crates/tabby-scheduler/src/repository.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
use std::{collections::HashSet, fs, path::Path, process::Command};

use anyhow::{anyhow, Result};
use tabby_common::{config::RepositoryConfig, path::repositories_dir};
use tracing::warn;

trait RepositoryExt {
fn sync(&self) -> Result<()>;
fn sync(&self);
}

impl RepositoryExt for RepositoryConfig {
fn sync(&self) -> Result<()> {
fn sync(&self) {
let dir = self.dir();
let mut finished = false;
if dir.exists() {
finished = pull_remote(dir.as_path())?;
finished = pull_remote(dir.as_path());
}

if !finished {
Expand All @@ -24,43 +23,47 @@ impl RepositoryExt for RepositoryConfig {
.arg("clone")
.args(["--depth", "1"])
.arg(&self.git_url)
.arg(dir)
.status()?;
.arg(&dir)
.status()
.expect("Failed to read status");

if let Some(code) = status.code() {
if code != 0 {
return Err(anyhow!(
"Failed to pull remote '{}'. Please check your repository configuration",
&self.git_url,
));
warn!(
"Failed to clone `{}`. Please check your repository configuration.",
&self.git_url
);
fs::remove_dir_all(&dir).expect("Failed to remove directory");
}
}
}

Ok(())
}
}

fn pull_remote(path: &Path) -> std::io::Result<bool> {
let status = Command::new("git").current_dir(path).arg("pull").status()?;
fn pull_remote(path: &Path) -> bool {
let status = Command::new("git")
.current_dir(path)
.arg("pull")
.status()
.expect("Failed to read status");

if let Some(code) = status.code() {
if code != 0 {
warn!(
"Failed to pull remote for `{:?}`, It will now be removed...",
path
);
fs::remove_dir_all(path)?;
return Ok(false);
fs::remove_dir_all(path).expect("Failed to remove directory");
return false;
}
};

Ok(true)
true
}

pub fn sync_repositories(repositories: &[RepositoryConfig]) -> Result<()> {
pub fn sync_repositories(repositories: &[RepositoryConfig]) {
// Ensure repositories_dir exist.
std::fs::create_dir_all(repositories_dir())?;
std::fs::create_dir_all(repositories_dir()).expect("Failed to create repositories directory");

let mut names = HashSet::new();
for repository in repositories {
Expand All @@ -70,22 +73,26 @@ pub fn sync_repositories(repositories: &[RepositoryConfig]) -> Result<()> {
panic!("Directory {} does not exist", repository.dir().display());
}
} else {
repository.sync()?;
repository.sync();
}
}

for file in fs::read_dir(repositories_dir())?.filter_map(Result::ok) {
let metadata = file.metadata()?;
for file in fs::read_dir(repositories_dir())
.expect("Failed to read repository dir")
.filter_map(Result::ok)
{
let metadata = file.metadata().expect("Failed to read metadata");
let filename = file.file_name();
if metadata.is_file() {
warn!("An unrelated file {:?} was found in repositories directory, It will now be removed...", filename);
// There shouldn't be any files under repositories dir.
fs::remove_file(file.path())?;
fs::remove_file(file.path())
.unwrap_or_else(|_| panic!("Failed to remove file {:?}", filename))
} else if metadata.is_dir() && !names.contains(&file.path()) {
warn!("An unrelated directory {:?} was found in repositories directory, It will now be removed...", file.path().display());
fs::remove_dir_all(file.path())?;
fs::remove_dir_all(file.path()).unwrap_or_else(|_| {
panic!("Failed to remove directory {:?}", file.path().display())
});
}
}

Ok(())
}

0 comments on commit 9822c2f

Please sign in to comment.