Skip to content

Commit

Permalink
feat(scheduler): remove unrelated file / directories under ~/.tabby/r…
Browse files Browse the repository at this point in the history
…epositories (#1047)
  • Loading branch information
wsxiaoys authored Dec 15, 2023
1 parent 45a2b5b commit 8fe0063
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
26 changes: 24 additions & 2 deletions crates/tabby-scheduler/src/repository.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
use std::process::Command;
use std::{collections::HashSet, fs, process::Command};

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

trait ConfigExt {
fn sync_repositories(&self) -> Result<()>;
}

impl ConfigExt for Config {
fn sync_repositories(&self) -> Result<()> {
let mut names = HashSet::new();
for repository in self.repositories.iter() {
names.insert(repository.name());
if repository.is_local_dir() {
if !repository.dir().exists() {
panic!("Directory {} does not exist", repository.dir().display());
Expand All @@ -19,6 +25,22 @@ impl ConfigExt for Config {
}
}

for file in fs::read_dir(repositories_dir())?.filter_map(Result::ok) {
let metadata = file.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())?;
} else if metadata.is_dir() {
let filename = filename.to_str().ok_or(anyhow!("Invalid file name"))?;
if !names.contains(filename) {
warn!("An unrelated directory {:?} was found in repositories directory, It will now be removed...", filename);
fs::remove_dir_all(file.path())?;
}
}
}

Ok(())
}
}
Expand Down
5 changes: 4 additions & 1 deletion website/docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@ Tabby server will look for a configuration file at `~/.tabby/config.toml` for ad
To enable repository level context for code completion, you can add the following to your configuration file:

```toml title="~/.tabby/config.toml"
# Index two repositories' source code as additional context for code completion.
# Index three repositories' source code as additional context for code completion.

[[repositories]]
name = "tabby"
git_url = "https://github.com/TabbyML/tabby.git"

# git through ssh protocol.
[[repositories]]
name = "CTranslate2"
git_url = "[email protected]:OpenNMT/CTranslate2.git"

# local directory is also supported!
[[repositories]]
name = "repository_a"
git_url = "file:///home/users/repository_a"
```

Expand Down

0 comments on commit 8fe0063

Please sign in to comment.