handle open error

This commit is contained in:
Nikita Galaiko 2023-11-14 15:19:22 +01:00 committed by GitButler
parent 5e2aa2f592
commit 9eb3cf9e0a
23 changed files with 64 additions and 110 deletions

View File

@ -65,11 +65,11 @@ impl App {
}
pub fn project_repository(&self) -> project_repository::Repository {
project_repository::Repository::try_from(&self.project).unwrap()
project_repository::Repository::open(&self.project).unwrap()
}
pub fn gb_repository(&self) -> gb_repository::Repository {
let project_repository = project_repository::Repository::try_from(&self.project)
let project_repository = project_repository::Repository::open(&self.project)
.expect("failed to open project repository");
let gb_repository = gb_repository::Repository::open(
&self.local_data_dir,

View File

@ -76,46 +76,6 @@ impl App {
Ok(())
}
pub fn list_sessions(
&self,
project_id: &ProjectId,
earliest_timestamp_ms: Option<u128>,
) -> Result<Vec<sessions::Session>> {
let sessions = self
.sessions_database
.list_by_project_id(project_id, earliest_timestamp_ms)?;
let project = self.projects.get(project_id)?;
let project_repository = project_repository::Repository::try_from(&project)?;
let user = self.users.get_user().context("failed to get user")?;
let gb_repository = gb_repository::Repository::open(
&self.local_data_dir,
&project_repository,
user.as_ref(),
)?;
// this is a hack to account for a case when we have a session created, but fs was never
// touched, so the wathcer never picked up the session
let current_session = gb_repository.get_current_session()?;
let have_to_index = matches!(
(current_session.as_ref(), sessions.first()),
(Some(_), None)
);
if !have_to_index {
return Ok(sessions);
}
let sessions_iter = gb_repository.get_sessions_iterator()?;
let mut sessions = sessions_iter.collect::<Result<Vec<_>, _>>()?;
self.sessions_database
.insert(project_id, &sessions.iter().collect::<Vec<_>>())?;
if let Some(session) = current_session {
self.sessions_database.insert(project_id, &[&session])?;
sessions.insert(0, session);
}
Ok(sessions)
}
pub fn list_session_files(
&self,
project_id: &ProjectId,
@ -129,7 +89,7 @@ impl App {
.context("session not found")?;
let user = self.users.get_user().context("failed to get user")?;
let project = self.projects.get(project_id)?;
let project_repository = project_repository::Repository::try_from(&project)?;
let project_repository = project_repository::Repository::open(&project)?;
let gb_repo = gb_repository::Repository::open(
&self.local_data_dir,
&project_repository,
@ -146,7 +106,7 @@ impl App {
pub fn mark_resolved(&self, project_id: &ProjectId, path: &str) -> Result<(), Error> {
let project = self.projects.get(project_id)?;
let project_repository = project_repository::Repository::try_from(&project)?;
let project_repository = project_repository::Repository::open(&project)?;
// mark file as resolved
conflicts::resolve(&project_repository, path)?;
Ok(())
@ -154,7 +114,7 @@ impl App {
pub fn fetch_from_target(&self, project_id: &ProjectId) -> Result<(), Error> {
let project = self.projects.get(project_id)?;
let project_repository = project_repository::Repository::try_from(&project)?;
let project_repository = project_repository::Repository::open(&project)?;
let user = self.users.get_user().context("failed to get user")?;
let gb_repo = gb_repository::Repository::open(
&self.local_data_dir,
@ -190,7 +150,7 @@ impl App {
context_lines: u32,
) -> Result<HashMap<path::PathBuf, String>, Error> {
let project = self.projects.get(project_id)?;
let project_repository = project_repository::Repository::try_from(&project)?;
let project_repository = project_repository::Repository::open(&project)?;
let diff = diff::workdir(
&project_repository.git_repository,
@ -226,7 +186,7 @@ impl App {
project_id: &ProjectId,
) -> Result<Vec<git::RemoteBranchName>, Error> {
let project = self.projects.get(project_id)?;
let project_repository = project_repository::Repository::try_from(&project)?;
let project_repository = project_repository::Repository::open(&project)?;
project_repository
.git_remote_branches()
.map_err(Error::Other)
@ -234,7 +194,7 @@ impl App {
pub fn git_head(&self, project_id: &ProjectId) -> Result<String, Error> {
let project = self.projects.get(project_id)?;
let project_repository = project_repository::Repository::try_from(&project)?;
let project_repository = project_repository::Repository::open(&project)?;
let head = project_repository
.get_head()
.context("failed to get repository head")?;

View File

@ -13,6 +13,7 @@ use super::{
impl From<UpsertError> for Error {
fn from(value: UpsertError) -> Self {
match value {
UpsertError::OpenProjectRepository(error) => Error::from(error),
UpsertError::GetUser(error) => Error::from(error),
UpsertError::GetProject(error) => Error::from(error),
UpsertError::Other(error) => {

View File

@ -42,6 +42,8 @@ pub enum UpsertError {
#[error(transparent)]
GetUser(#[from] users::GetError),
#[error(transparent)]
OpenProjectRepository(#[from] project_repository::OpenError),
#[error(transparent)]
Other(#[from] anyhow::Error),
}
@ -54,8 +56,7 @@ pub enum ListError {
impl Controller {
pub fn upsert(&self, bookmark: &Bookmark) -> Result<Option<Bookmark>, UpsertError> {
let project = self.projects.get(&bookmark.project_id)?;
let project_repository = project_repository::Repository::try_from(&project)
.context("failed to open project repository")?;
let project_repository = project_repository::Repository::open(&project)?;
let user = self.users.get_user()?;
let gb_repository = gb_repository::Repository::open(
&self.local_data_dir,

View File

@ -169,8 +169,7 @@ pub async fn project_flush_and_push(handle: tauri::AppHandle, id: &str) -> Resul
let project = projects.get(&id).context("failed to get project")?;
let user = users.get_user()?;
let project_repository =
project_repository::Repository::try_from(&project).context("failed to open repository")?;
let project_repository = project_repository::Repository::open(&project)?;
let gb_repo =
gb_repository::Repository::open(&local_data_dir, &project_repository, user.as_ref())
.context("failed to open repository")?;

View File

@ -369,7 +369,7 @@ impl Repository {
match sessions::Session::try_from(reader) {
Result::Ok(session) => Ok(session),
Err(sessions::SessionError::NoSession) => {
let project_repository = project_repository::Repository::try_from(&self.project)
let project_repository = project_repository::Repository::open(&self.project)
.context("failed to open project repository")?;
let session = self
.create_current_session(&project_repository)

View File

@ -40,30 +40,19 @@ impl From<OpenError> for crate::error::Error {
}
}
impl TryFrom<projects::Project> for Repository {
type Error = OpenError;
fn try_from(project: projects::Project) -> Result<Self, Self::Error> {
let git_repository = git::Repository::open(&project.path).map_err(|error| match error {
git::Error::NotFound(_) => OpenError::NotFound(project.path.clone()),
other => OpenError::Other(other.into()),
})?;
Ok(Self {
git_repository,
project,
})
}
}
impl TryFrom<&projects::Project> for Repository {
type Error = OpenError;
fn try_from(project: &projects::Project) -> Result<Self, Self::Error> {
Self::try_from(project.clone())
}
}
impl Repository {
pub fn open(project: &projects::Project) -> Result<Self, OpenError> {
git::Repository::open(&project.path)
.map_err(|error| match error {
git::Error::NotFound(_) => OpenError::NotFound(project.path.clone()),
other => OpenError::Other(other.into()),
})
.map(|git_repository| Self {
git_repository,
project: project.clone(),
})
}
pub fn path(&self) -> &path::Path {
path::Path::new(&self.project.path)
}

View File

@ -48,6 +48,7 @@ impl From<controller::AddError> for Error {
code: Code::Projects,
message: "Project already exists".to_string(),
},
controller::AddError::OpenProjectRepository(error) => error.into(),
controller::AddError::NotADirectory => Error::UserError {
code: Code::Projects,
message: "Not a directory".to_string(),

View File

@ -77,8 +77,7 @@ impl Controller {
// create all required directories to avoid racing later
let user = self.users.get_user()?;
let project_repository = project_repository::Repository::try_from(&project)
.context("failed to open repository")?;
let project_repository = project_repository::Repository::open(&project)?;
gb_repository::Repository::open(&self.local_data_dir, &project_repository, user.as_ref())
.context("failed to open repository")?;
@ -219,5 +218,7 @@ pub enum AddError {
#[error(transparent)]
User(#[from] users::GetError),
#[error(transparent)]
OpenProjectRepository(#[from] project_repository::OpenError),
#[error(transparent)]
Other(#[from] anyhow::Error),
}

View File

@ -13,6 +13,7 @@ impl From<ListError> for Error {
match value {
ListError::UsersError(error) => Error::from(error),
ListError::ProjectsError(error) => Error::from(error),
ListError::ProjectRepositoryError(error) => Error::from(error),
ListError::Other(error) => {
tracing::error!(?error);
Error::Unknown

View File

@ -35,6 +35,8 @@ pub enum ListError {
#[error(transparent)]
ProjectsError(#[from] projects::GetError),
#[error(transparent)]
ProjectRepositoryError(#[from] project_repository::OpenError),
#[error(transparent)]
UsersError(#[from] users::GetError),
#[error(transparent)]
Other(#[from] anyhow::Error),
@ -51,8 +53,7 @@ impl Controller {
.list_by_project_id(project_id, earliest_timestamp_ms)?;
let project = self.projects.get(project_id)?;
let project_repository = project_repository::Repository::try_from(&project)
.context("failed to open project repository")?;
let project_repository = project_repository::Repository::open(&project)?;
let user = self.users.get_user()?;
let gb_repository = gb_repository::Repository::open(
&self.local_data_dir,

View File

@ -83,7 +83,7 @@ pub struct Case<'a> {
impl<'a> Case<'a> {
fn new(suite: &'a Suite, project: projects::Project) -> Case<'a> {
let project_repository = project_repository::Repository::try_from(&project)
let project_repository = project_repository::Repository::open(&project)
.expect("failed to create project repository");
let gb_repository =
gb_repository::Repository::open(&suite.local_app_data, &project_repository, None)
@ -108,7 +108,7 @@ impl<'a> Case<'a> {
.projects
.get(&self.project.id)
.expect("failed to get project");
let project_repository = project_repository::Repository::try_from(&project)
let project_repository = project_repository::Repository::open(&project)
.expect("failed to create project repository");
let user = self.suite.users.get_user().expect("failed to get user");
let credentials = git::credentials::Factory::new(

View File

@ -131,7 +131,7 @@ impl Controller {
branch_name: &git::BranchName,
) -> Result<bool, Error> {
let project = self.projects.get(project_id)?;
let project_repository = project_repository::Repository::try_from(&project)?;
let project_repository = project_repository::Repository::open(&project)?;
let user = self.users.get_user().context("failed to get user")?;
let gb_repository = gb_repository::Repository::open(
&self.local_data_dir,
@ -149,7 +149,7 @@ impl Controller {
branch_id: &BranchId,
) -> Result<bool, Error> {
let project = self.projects.get(project_id)?;
let project_repository = project_repository::Repository::try_from(&project)?;
let project_repository = project_repository::Repository::open(&project)?;
let user = self.users.get_user().context("failed to get user")?;
let gb_repository = gb_repository::Repository::open(
&self.local_data_dir,
@ -241,7 +241,7 @@ impl Controller {
project_id: &ProjectId,
) -> Result<Option<super::BaseBranch>, Error> {
let project = self.projects.get(project_id)?;
let project_repository = project_repository::Repository::try_from(&project)?;
let project_repository = project_repository::Repository::open(&project)?;
let user = self.users.get_user().context("failed to get user")?;
let gb_repository = gb_repository::Repository::open(
&self.local_data_dir,
@ -260,7 +260,7 @@ impl Controller {
commit_oid: git::Oid,
) -> Result<Vec<RemoteBranchFile>, Error> {
let project = self.projects.get(project_id)?;
let project_repository = project_repository::Repository::try_from(&project)?;
let project_repository = project_repository::Repository::open(&project)?;
let commit = project_repository
.git_repository
.find_commit(commit_oid)
@ -278,7 +278,7 @@ impl Controller {
let user = self.users.get_user().context("failed to get user")?;
let project_repository = project_repository::Repository::try_from(&project)?;
let project_repository = project_repository::Repository::open(&project)?;
let gb_repository = gb_repository::Repository::open(
&self.local_data_dir,
@ -514,7 +514,7 @@ impl Controller {
) -> Result<T, Error>,
) -> Result<T, Error> {
let project = self.projects.get(project_id)?;
let project_repository = project_repository::Repository::try_from(&project)?;
let project_repository = project_repository::Repository::open(&project)?;
let user = self.users.get_user().context("failed to get user")?;
let gb_repository = gb_repository::Repository::open(
&self.local_data_dir,
@ -574,7 +574,7 @@ impl Controller {
project_id: &ProjectId,
) -> Result<Vec<super::RemoteBranch>, Error> {
let project = self.projects.get(project_id)?;
let project_repository = project_repository::Repository::try_from(&project)?;
let project_repository = project_repository::Repository::open(&project)?;
let user = self.users.get_user().context("failed to get user")?;
let gb_repository = gb_repository::Repository::open(
&self.local_data_dir,

View File

@ -78,8 +78,8 @@ impl HandlerInner {
anyhow::bail!("sync disabled");
}
let project_repository = project_repository::Repository::try_from(&project)
.context("failed to open repository")?;
let project_repository =
project_repository::Repository::open(&project).context("failed to open repository")?;
let gb_repo = gb_repository::Repository::open(
&self.local_data_dir,
&project_repository,

View File

@ -78,7 +78,7 @@ impl HandlerInner {
.projects
.get(project_id)
.context("failed to get project")?;
let project_repository = project_repository::Repository::try_from(&project)
let project_repository = project_repository::Repository::open(&project)
.context("failed to open repository. Make sure the project is configured correctly.")?;
let gb_repo = gb_repository::Repository::open(
&self.local_data_dir,

View File

@ -44,8 +44,8 @@ impl Handler {
.context("failed to get project")?;
let user = self.users.get_user()?;
let project_repository = project_repository::Repository::try_from(&project)
.context("failed to open repository")?;
let project_repository =
project_repository::Repository::open(&project).context("failed to open repository")?;
let gb_repo = gb_repository::Repository::open(
&self.local_data_dir,
&project_repository,

View File

@ -41,7 +41,7 @@ impl Handler {
.get(project_id)
.context("failed to get project")?;
let project_repository = project_repository::Repository::try_from(&project)
let project_repository = project_repository::Repository::open(&project)
.context("failed to open project repository for project")?;
match path.as_ref().to_str().unwrap() {

View File

@ -71,8 +71,8 @@ impl Handler {
pub fn reindex(&self, project_id: &ProjectId) -> Result<Vec<events::Event>> {
let user = self.users.get_user()?;
let project = self.projects.get(project_id)?;
let project_repository = project_repository::Repository::try_from(&project)
.context("failed to open repository")?;
let project_repository =
project_repository::Repository::open(&project).context("failed to open repository")?;
let gb_repository = gb_repository::Repository::open(
&self.local_data_dir,
&project_repository,
@ -95,8 +95,8 @@ impl Handler {
) -> Result<Vec<events::Event>> {
let user = self.users.get_user()?;
let project = self.projects.get(project_id)?;
let project_repository = project_repository::Repository::try_from(&project)
.context("failed to open repository")?;
let project_repository =
project_repository::Repository::open(&project).context("failed to open repository")?;
let gb_repository = gb_repository::Repository::open(
&self.local_data_dir,
&project_repository,

View File

@ -86,7 +86,7 @@ impl Handler {
.get(project_id)
.context("failed to get project")?;
let project_repository = project_repository::Repository::try_from(&project)
let project_repository = project_repository::Repository::open(&project)
.with_context(|| "failed to open project repository for project")?;
let user = self.users.get_user().context("failed to get user")?;

View File

@ -68,8 +68,8 @@ impl HandlerInner {
pub fn handle(&self, project_id: &ProjectId) -> Result<Vec<events::Event>> {
let user = self.users.get_user()?;
let project = self.projects.get(project_id)?;
let project_repository = project_repository::Repository::try_from(&project)
.context("failed to open repository")?;
let project_repository =
project_repository::Repository::open(&project).context("failed to open repository")?;
let gb_repo = gb_repository::Repository::open(
&self.local_data_dir,
&project_repository,

View File

@ -77,8 +77,8 @@ impl HandlerInner {
.context("failed to get project")?;
let user = self.users.get_user()?;
let project_repository = project_repository::Repository::try_from(&project)
.context("failed to open repository")?;
let project_repository =
project_repository::Repository::open(&project).context("failed to open repository")?;
if project_repository.project().is_sync_enabled()
&& project_repository.project().has_code_url()

View File

@ -45,7 +45,7 @@ impl Handler {
let user = self.users.get_user()?;
let project = self.projects.get(project_id)?;
let project_repository = match project_repository::Repository::try_from(&project) {
let project_repository = match project_repository::Repository::open(&project) {
Ok(project_repository) => Ok(project_repository),
Err(project_repository::OpenError::NotFound(_)) => return Ok(vec![]),
Err(error) => Err(error),

View File

@ -19,7 +19,7 @@ mod init {
std::fs::write(project.path.join("file"), "content").unwrap();
std::fs::hard_link(project.path.join("file"), project.path.join("link")).unwrap();
let project_repository = project_repository::Repository::try_from(project).unwrap();
let project_repository = project_repository::Repository::open(&project).unwrap();
gb_repository::Repository::open(&data_dir, &project_repository, None).unwrap();
}
@ -40,7 +40,7 @@ mod init {
std::os::unix::fs::symlink(project.path.join("dir"), project.path.join("dir_link"))
.unwrap();
let project_repository = project_repository::Repository::try_from(project).unwrap();
let project_repository = project_repository::Repository::open(&project).unwrap();
gb_repository::Repository::open(&data_dir, &project_repository, None).unwrap();
}
@ -66,7 +66,7 @@ mod init {
)
.unwrap();
let project_repository = project_repository::Repository::try_from(project).unwrap();
let project_repository = project_repository::Repository::open(&project).unwrap();
gb_repository::Repository::open(&data_dir, &project_repository, None).unwrap();
}
@ -86,7 +86,7 @@ mod flush {
.add(test_project.path())
.expect("failed to add project");
let project_repository = project_repository::Repository::try_from(&project).unwrap();
let project_repository = project_repository::Repository::open(&project).unwrap();
let gb_repo =
gb_repository::Repository::open(&data_dir, &project_repository, None).unwrap();
@ -108,7 +108,7 @@ mod flush {
.add(test_project.path())
.expect("failed to add project");
let project_repository = project_repository::Repository::try_from(&project).unwrap();
let project_repository = project_repository::Repository::open(&project).unwrap();
let gb_repo =
gb_repository::Repository::open(&data_dir, &project_repository, None).unwrap();