extract create_virtual_branch

This commit is contained in:
Nikita Galaiko 2023-08-08 13:21:42 +02:00
parent fab6faeec9
commit 6797cd27e9
4 changed files with 50 additions and 42 deletions

View File

@ -1,6 +1,6 @@
use std::{collections::HashMap, ops, path, sync, thread, time};
use anyhow::{bail, Context, Result};
use anyhow::{Context, Result};
use futures::executor::block_on;
use tauri::{AppHandle, Manager};
use tokio::sync::{Mutex, Semaphore};
@ -332,31 +332,6 @@ impl App {
Ok(())
}
pub async fn create_virtual_branch(
&self,
project_id: &str,
create: &virtual_branches::branch::BranchCreateRequest,
) -> Result<()> {
let gb_repository = self.gb_repository(project_id)?;
let project = self.gb_project(project_id)?;
let project_repository = project_repository::Repository::open(&project)
.context("failed to open project repository")?;
if conflicts::is_resolving(&project_repository) {
bail!("cannot create a branch, project is in a conflicted state");
}
let mut semaphores = self.vbranch_semaphores.lock().await;
let semaphore = semaphores
.entry(project_id.to_string())
.or_insert_with(|| Semaphore::new(1));
let _permit = semaphore.acquire().await?;
virtual_branches::create_virtual_branch(&gb_repository, create)?;
Ok(())
}
pub async fn create_virtual_branch_from_branch(
&self,
project_id: &str,

View File

@ -524,20 +524,6 @@ async fn list_bookmarks(
Ok(bookmarks)
}
#[timed(duration(printer = "debug!"))]
#[tauri::command(async)]
async fn create_virtual_branch(
handle: tauri::AppHandle,
project_id: &str,
branch: virtual_branches::branch::BranchCreateRequest,
) -> Result<(), Error> {
let app = handle.state::<app::App>();
app.create_virtual_branch(project_id, &branch)
.await
.context("failed to create virtual branch")?;
Ok(())
}
#[timed(duration(printer = "debug!"))]
#[tauri::command(async)]
async fn create_virtual_branch_from_branch(
@ -939,7 +925,7 @@ fn main() {
upsert_bookmark,
list_bookmarks,
virtual_branches::commands::list_virtual_branches,
create_virtual_branch,
virtual_branches::commands::create_virtual_branch,
virtual_branches::commands::commit_virtual_branch,
get_base_branch_data,
set_base_branch,

View File

@ -35,3 +35,18 @@ pub async fn list_virtual_branches(
.context("failed to list virtual branches")?;
Ok(branches)
}
#[timed(duration(printer = "debug!"))]
#[tauri::command(async)]
pub async fn create_virtual_branch(
handle: tauri::AppHandle,
project_id: &str,
branch: super::branch::BranchCreateRequest,
) -> Result<(), Error> {
handle
.state::<Controller>()
.create_virtual_branch(project_id, &branch)
.await
.context("failed to create virtual branch")?;
Ok(())
}

View File

@ -4,7 +4,7 @@ use anyhow::Context;
use tauri::AppHandle;
use tokio::sync::Semaphore;
use crate::{gb_repository, projects, users};
use crate::{gb_repository, project_repository::conflicts, projects, users};
pub struct Controller {
local_data_dir: path::PathBuf,
@ -16,6 +16,8 @@ pub struct Controller {
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("project is in a conflicted state")]
Conflicting,
#[error(transparent)]
LockError(#[from] tokio::sync::AcquireError),
#[error(transparent)]
@ -93,6 +95,36 @@ impl Controller {
Ok(branches)
}
pub async fn create_virtual_branch(
&self,
project_id: &str,
create: &super::branch::BranchCreateRequest,
) -> Result<(), Error> {
let project = self
.projects_storage
.get_project(project_id)
.context("failed to get project")?
.context("project not found")?;
let project_repository = project
.as_ref()
.try_into()
.context("failed to open project repository")?;
let gb_repository = self.open_gb_repository(project_id)?;
if conflicts::is_resolving(&project_repository) {
return Err(Error::Conflicting);
}
let mut semaphores = self.semaphores.lock().await;
let semaphore = semaphores
.entry(project_id.to_string())
.or_insert_with(|| Semaphore::new(1));
let _permit = semaphore.acquire().await?;
super::create_virtual_branch(&gb_repository, create)?;
Ok(())
}
fn open_gb_repository(&self, project_id: &str) -> Result<gb_repository::Repository, Error> {
gb_repository::Repository::open(
self.local_data_dir.clone(),