mirror of
https://github.com/gitbutlerapp/gitbutler.git
synced 2024-12-29 12:33:49 +03:00
make watchers use anyhow
This commit is contained in:
parent
d2b85e614d
commit
bc6ac42a71
@ -1,6 +1,7 @@
|
||||
use crate::deltas::{get_current_file_deltas, save_current_file_deltas, Delta, TextDocument};
|
||||
use crate::projects::project::Project;
|
||||
use crate::{butler, events, sessions};
|
||||
use anyhow::Result;
|
||||
use git2::Repository;
|
||||
use notify::{Config, EventKind, RecommendedWatcher, RecursiveMode, Watcher};
|
||||
use std::path::Path;
|
||||
@ -12,26 +13,7 @@ use std::{collections::HashMap, sync::Mutex};
|
||||
#[derive(Default)]
|
||||
pub struct WatcherCollection(Mutex<HashMap<String, RecommendedWatcher>>);
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum UnwatchError {
|
||||
UnwatchError(notify::Error),
|
||||
}
|
||||
|
||||
impl std::fmt::Display for UnwatchError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
UnwatchError::UnwatchError(e) => write!(f, "Unwatch error: {}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<notify::Error> for UnwatchError {
|
||||
fn from(error: notify::Error) -> Self {
|
||||
UnwatchError::UnwatchError(error)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn unwatch(watchers: &WatcherCollection, project: Project) -> Result<(), UnwatchError> {
|
||||
pub fn unwatch(watchers: &WatcherCollection, project: Project) -> Result<()> {
|
||||
let mut watchers = watchers.0.lock().unwrap();
|
||||
if let Some(mut watcher) = watchers.remove(&project.path) {
|
||||
watcher.unwatch(Path::new(&project.path))?;
|
||||
@ -39,38 +21,11 @@ pub fn unwatch(watchers: &WatcherCollection, project: Project) -> Result<(), Unw
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum WatchError {
|
||||
GitError(git2::Error),
|
||||
WatchError(notify::Error),
|
||||
}
|
||||
|
||||
impl std::fmt::Display for WatchError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
WatchError::GitError(e) => write!(f, "Git error: {}", e),
|
||||
WatchError::WatchError(e) => write!(f, "Watch error: {}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<notify::Error> for WatchError {
|
||||
fn from(error: notify::Error) -> Self {
|
||||
WatchError::WatchError(error)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<git2::Error> for WatchError {
|
||||
fn from(error: git2::Error) -> Self {
|
||||
WatchError::GitError(error)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn watch<R: tauri::Runtime>(
|
||||
window: tauri::Window<R>,
|
||||
watchers: &WatcherCollection,
|
||||
project: Project,
|
||||
) -> Result<(), WatchError> {
|
||||
) -> Result<()> {
|
||||
log::info!("Watching deltas for {}", project.path);
|
||||
let project_path = Path::new(&project.path);
|
||||
|
||||
|
@ -1,44 +1,18 @@
|
||||
use crate::{butler, events, projects::project::Project, sessions};
|
||||
use git2::Repository;
|
||||
use anyhow::Result;
|
||||
use std::{
|
||||
thread,
|
||||
time::{Duration, SystemTime},
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum WatchError {
|
||||
GitError(git2::Error),
|
||||
IOError(std::io::Error),
|
||||
}
|
||||
|
||||
impl std::fmt::Display for WatchError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
WatchError::GitError(e) => write!(f, "Git error: {}", e),
|
||||
WatchError::IOError(e) => write!(f, "IO error: {}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<git2::Error> for WatchError {
|
||||
fn from(error: git2::Error) -> Self {
|
||||
Self::GitError(error)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::io::Error> for WatchError {
|
||||
fn from(error: std::io::Error) -> Self {
|
||||
Self::IOError(error)
|
||||
}
|
||||
}
|
||||
|
||||
const FIVE_MINUTES: u64 = Duration::new(5 * 60, 0).as_secs();
|
||||
const ONE_HOUR: u64 = Duration::new(60 * 60, 0).as_secs();
|
||||
|
||||
pub fn watch<R: tauri::Runtime>(
|
||||
window: tauri::Window<R>,
|
||||
project: Project,
|
||||
) -> Result<(), WatchError> {
|
||||
) -> Result<() > {
|
||||
let repo = git2::Repository::open(&project.path)?;
|
||||
thread::spawn(move || loop {
|
||||
match repo.revparse_single(format!("refs/{}/current", butler::refname()).as_str()) {
|
||||
|
@ -3,84 +3,20 @@ mod git;
|
||||
|
||||
pub use self::delta::WatcherCollection;
|
||||
use crate::projects::project::Project;
|
||||
use serde::Serialize;
|
||||
use anyhow::Result;
|
||||
use tauri::{Runtime, Window};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum WatchError {
|
||||
WatchDeltaError(delta::WatchError),
|
||||
WatchGitError(git::WatchError),
|
||||
}
|
||||
|
||||
impl From<delta::WatchError> for WatchError {
|
||||
fn from(error: delta::WatchError) -> Self {
|
||||
WatchError::WatchDeltaError(error)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<git::WatchError> for WatchError {
|
||||
fn from(error: git::WatchError) -> Self {
|
||||
WatchError::WatchGitError(error)
|
||||
}
|
||||
}
|
||||
|
||||
impl Serialize for WatchError {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: serde::Serializer,
|
||||
{
|
||||
serializer.serialize_str(&format!("{:?}", self))
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for WatchError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
WatchError::WatchDeltaError(error) => write!(f, "watch delta error: {}", error),
|
||||
WatchError::WatchGitError(error) => write!(f, "watch git error: {}", error),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn watch<R: Runtime>(
|
||||
window: Window<R>,
|
||||
watchers: &WatcherCollection,
|
||||
project: &Project,
|
||||
) -> Result<(), WatchError> {
|
||||
) -> Result<()> {
|
||||
self::delta::watch(window.clone(), watchers, project.clone())?;
|
||||
self::git::watch(window.clone(), project.clone())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum UnwatchError {
|
||||
DeltaError(delta::UnwatchError),
|
||||
}
|
||||
|
||||
impl std::fmt::Display for UnwatchError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
UnwatchError::DeltaError(error) => write!(f, "unwatch delta error: {}", error),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<delta::UnwatchError> for UnwatchError {
|
||||
fn from(error: delta::UnwatchError) -> Self {
|
||||
UnwatchError::DeltaError(error)
|
||||
}
|
||||
}
|
||||
|
||||
impl Serialize for UnwatchError {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: serde::Serializer,
|
||||
{
|
||||
serializer.serialize_str(&format!("{:?}", self))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn unwatch(watchers: &WatcherCollection, project: Project) -> Result<(), UnwatchError> {
|
||||
pub fn unwatch(watchers: &WatcherCollection, project: Project) -> Result<()> {
|
||||
delta::unwatch(watchers, project)?;
|
||||
// TODO: how to unwatch git ?
|
||||
Ok(())
|
||||
|
Loading…
Reference in New Issue
Block a user