mirror of
https://github.com/gitbutlerapp/gitbutler.git
synced 2024-08-17 00:00:31 +03:00
simplify virtual branches controller deps graph
users controller is not a real dependency
This commit is contained in:
parent
6add0a40cc
commit
3dded08458
@ -17,29 +17,22 @@ use super::{
|
||||
use crate::{
|
||||
git, project_repository,
|
||||
projects::{self, ProjectId},
|
||||
users,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Controller {
|
||||
projects: projects::Controller,
|
||||
users: users::Controller,
|
||||
helper: git::credentials::Helper,
|
||||
|
||||
semaphore: Arc<Semaphore>,
|
||||
}
|
||||
|
||||
impl Controller {
|
||||
pub fn new(
|
||||
projects: projects::Controller,
|
||||
users: users::Controller,
|
||||
helper: git::credentials::Helper,
|
||||
) -> Self {
|
||||
pub fn new(projects: projects::Controller, helper: git::credentials::Helper) -> Self {
|
||||
Self {
|
||||
semaphore: Arc::new(Semaphore::new(1)),
|
||||
|
||||
projects,
|
||||
users,
|
||||
helper,
|
||||
}
|
||||
}
|
||||
@ -53,7 +46,7 @@ impl Controller {
|
||||
run_hooks: bool,
|
||||
) -> Result<git2::Oid> {
|
||||
let _permit = self.semaphore.acquire().await;
|
||||
self.with_verify_branch(project_id, |project_repository, _| {
|
||||
self.with_verify_branch(project_id, |project_repository| {
|
||||
let snapshot_tree = project_repository.project().prepare_snapshot();
|
||||
let result =
|
||||
super::commit(project_repository, branch_id, message, ownership, run_hooks)
|
||||
@ -86,7 +79,7 @@ impl Controller {
|
||||
) -> Result<(Vec<super::VirtualBranch>, Vec<git::diff::FileDiff>)> {
|
||||
let _permit = self.semaphore.acquire().await;
|
||||
|
||||
self.with_verify_branch(project_id, |project_repository, _| {
|
||||
self.with_verify_branch(project_id, |project_repository| {
|
||||
super::list_virtual_branches(project_repository).map_err(Into::into)
|
||||
})
|
||||
}
|
||||
@ -98,7 +91,7 @@ impl Controller {
|
||||
) -> Result<BranchId> {
|
||||
let _permit = self.semaphore.acquire().await;
|
||||
|
||||
self.with_verify_branch(project_id, |project_repository, _| {
|
||||
self.with_verify_branch(project_id, |project_repository| {
|
||||
let branch_id = super::create_virtual_branch(project_repository, create)?.id;
|
||||
Ok(branch_id)
|
||||
})
|
||||
@ -111,7 +104,7 @@ impl Controller {
|
||||
) -> Result<BranchId> {
|
||||
let _permit = self.semaphore.acquire().await;
|
||||
|
||||
self.with_verify_branch(project_id, |project_repository, _| {
|
||||
self.with_verify_branch(project_id, |project_repository| {
|
||||
super::create_virtual_branch_from_branch(project_repository, branch).map_err(Into::into)
|
||||
})
|
||||
}
|
||||
@ -162,7 +155,7 @@ impl Controller {
|
||||
) -> Result<()> {
|
||||
let _permit = self.semaphore.acquire().await;
|
||||
|
||||
self.with_verify_branch(project_id, |project_repository, _| {
|
||||
self.with_verify_branch(project_id, |project_repository| {
|
||||
let _ = project_repository
|
||||
.project()
|
||||
.create_snapshot(SnapshotDetails::new(OperationKind::MergeUpstream));
|
||||
@ -173,7 +166,7 @@ impl Controller {
|
||||
pub async fn update_base_branch(&self, project_id: ProjectId) -> Result<Vec<ReferenceName>> {
|
||||
let _permit = self.semaphore.acquire().await;
|
||||
|
||||
self.with_verify_branch(project_id, |project_repository, _| {
|
||||
self.with_verify_branch(project_id, |project_repository| {
|
||||
let _ = project_repository
|
||||
.project()
|
||||
.create_snapshot(SnapshotDetails::new(OperationKind::UpdateWorkspaceBase));
|
||||
@ -195,7 +188,7 @@ impl Controller {
|
||||
) -> Result<()> {
|
||||
let _permit = self.semaphore.acquire().await;
|
||||
|
||||
self.with_verify_branch(project_id, |project_repository, _| {
|
||||
self.with_verify_branch(project_id, |project_repository| {
|
||||
let snapshot_tree = project_repository.project().prepare_snapshot();
|
||||
let old_branch = project_repository
|
||||
.project()
|
||||
@ -222,7 +215,7 @@ impl Controller {
|
||||
) -> Result<()> {
|
||||
let _permit = self.semaphore.acquire().await;
|
||||
|
||||
self.with_verify_branch(project_id, |project_repository, _| {
|
||||
self.with_verify_branch(project_id, |project_repository| {
|
||||
super::delete_branch(project_repository, branch_id)
|
||||
})
|
||||
}
|
||||
@ -234,7 +227,7 @@ impl Controller {
|
||||
) -> Result<()> {
|
||||
let _permit = self.semaphore.acquire().await;
|
||||
|
||||
self.with_verify_branch(project_id, |project_repository, _| {
|
||||
self.with_verify_branch(project_id, |project_repository| {
|
||||
let _ = project_repository
|
||||
.project()
|
||||
.create_snapshot(SnapshotDetails::new(OperationKind::DiscardHunk));
|
||||
@ -245,7 +238,7 @@ impl Controller {
|
||||
pub async fn reset_files(&self, project_id: ProjectId, files: &Vec<String>) -> Result<()> {
|
||||
let _permit = self.semaphore.acquire().await;
|
||||
|
||||
self.with_verify_branch(project_id, |project_repository, _| {
|
||||
self.with_verify_branch(project_id, |project_repository| {
|
||||
let _ = project_repository
|
||||
.project()
|
||||
.create_snapshot(SnapshotDetails::new(OperationKind::DiscardFile));
|
||||
@ -262,7 +255,7 @@ impl Controller {
|
||||
) -> Result<git2::Oid> {
|
||||
let _permit = self.semaphore.acquire().await;
|
||||
|
||||
self.with_verify_branch(project_id, |project_repository, _| {
|
||||
self.with_verify_branch(project_id, |project_repository| {
|
||||
let _ = project_repository
|
||||
.project()
|
||||
.create_snapshot(SnapshotDetails::new(OperationKind::AmendCommit));
|
||||
@ -280,7 +273,7 @@ impl Controller {
|
||||
) -> Result<git2::Oid> {
|
||||
let _permit = self.semaphore.acquire().await;
|
||||
|
||||
self.with_verify_branch(project_id, |project_repository, _| {
|
||||
self.with_verify_branch(project_id, |project_repository| {
|
||||
let _ = project_repository
|
||||
.project()
|
||||
.create_snapshot(SnapshotDetails::new(OperationKind::MoveCommitFile));
|
||||
@ -303,7 +296,7 @@ impl Controller {
|
||||
) -> Result<()> {
|
||||
let _permit = self.semaphore.acquire().await;
|
||||
|
||||
self.with_verify_branch(project_id, |project_repository, _| {
|
||||
self.with_verify_branch(project_id, |project_repository| {
|
||||
let snapshot_tree = project_repository.project().prepare_snapshot();
|
||||
let result: Result<()> =
|
||||
super::undo_commit(project_repository, branch_id, commit_oid).map_err(Into::into);
|
||||
@ -327,7 +320,7 @@ impl Controller {
|
||||
) -> Result<()> {
|
||||
let _permit = self.semaphore.acquire().await;
|
||||
|
||||
self.with_verify_branch(project_id, |project_repository, _| {
|
||||
self.with_verify_branch(project_id, |project_repository| {
|
||||
let _ = project_repository
|
||||
.project()
|
||||
.create_snapshot(SnapshotDetails::new(OperationKind::InsertBlankCommit));
|
||||
@ -345,7 +338,7 @@ impl Controller {
|
||||
) -> Result<()> {
|
||||
let _permit = self.semaphore.acquire().await;
|
||||
|
||||
self.with_verify_branch(project_id, |project_repository, _| {
|
||||
self.with_verify_branch(project_id, |project_repository| {
|
||||
let _ = project_repository
|
||||
.project()
|
||||
.create_snapshot(SnapshotDetails::new(OperationKind::ReorderCommit));
|
||||
@ -362,7 +355,7 @@ impl Controller {
|
||||
) -> Result<()> {
|
||||
let _permit = self.semaphore.acquire().await;
|
||||
|
||||
self.with_verify_branch(project_id, |project_repository, _| {
|
||||
self.with_verify_branch(project_id, |project_repository| {
|
||||
let _ = project_repository
|
||||
.project()
|
||||
.create_snapshot(SnapshotDetails::new(OperationKind::UndoCommit));
|
||||
@ -379,7 +372,7 @@ impl Controller {
|
||||
) -> Result<ReferenceName> {
|
||||
let _permit = self.semaphore.acquire().await;
|
||||
|
||||
self.with_verify_branch(project_id, |project_repository, _| {
|
||||
self.with_verify_branch(project_id, |project_repository| {
|
||||
let snapshot_tree = project_repository.project().prepare_snapshot();
|
||||
let result = super::convert_to_real_branch(
|
||||
project_repository,
|
||||
@ -405,7 +398,7 @@ impl Controller {
|
||||
) -> Result<()> {
|
||||
let _permit = self.semaphore.acquire().await;
|
||||
let helper = self.helper.clone();
|
||||
self.with_verify_branch_async(project_id, move |project_repository, _| {
|
||||
self.with_verify_branch_async(project_id, move |project_repository| {
|
||||
super::push(project_repository, branch_id, with_force, &helper, askpass)
|
||||
})?
|
||||
.await?
|
||||
@ -438,7 +431,7 @@ impl Controller {
|
||||
) -> Result<()> {
|
||||
let _permit = self.semaphore.acquire().await;
|
||||
|
||||
self.with_verify_branch(project_id, |project_repository, _| {
|
||||
self.with_verify_branch(project_id, |project_repository| {
|
||||
let _ = project_repository
|
||||
.project()
|
||||
.create_snapshot(SnapshotDetails::new(OperationKind::SquashCommit));
|
||||
@ -454,7 +447,7 @@ impl Controller {
|
||||
message: &str,
|
||||
) -> Result<()> {
|
||||
let _permit = self.semaphore.acquire().await;
|
||||
self.with_verify_branch(project_id, |project_repository, _| {
|
||||
self.with_verify_branch(project_id, |project_repository| {
|
||||
let _ = project_repository
|
||||
.project()
|
||||
.create_snapshot(SnapshotDetails::new(OperationKind::UpdateCommitMessage));
|
||||
@ -530,7 +523,7 @@ impl Controller {
|
||||
) -> Result<()> {
|
||||
let _permit = self.semaphore.acquire().await;
|
||||
|
||||
self.with_verify_branch(project_id, |project_repository, _| {
|
||||
self.with_verify_branch(project_id, |project_repository| {
|
||||
let _ = project_repository
|
||||
.project()
|
||||
.create_snapshot(SnapshotDetails::new(OperationKind::MoveCommit));
|
||||
@ -543,28 +536,24 @@ impl Controller {
|
||||
fn with_verify_branch<T>(
|
||||
&self,
|
||||
project_id: ProjectId,
|
||||
action: impl FnOnce(&project_repository::Repository, Option<&users::User>) -> Result<T>,
|
||||
action: impl FnOnce(&project_repository::Repository) -> Result<T>,
|
||||
) -> Result<T> {
|
||||
let project = self.projects.get(project_id)?;
|
||||
let project_repository = project_repository::Repository::open(&project)?;
|
||||
let user = self.users.get_user()?;
|
||||
super::integration::verify_branch(&project_repository)?;
|
||||
action(&project_repository, user.as_ref())
|
||||
action(&project_repository)
|
||||
}
|
||||
|
||||
fn with_verify_branch_async<T: Send + 'static>(
|
||||
&self,
|
||||
project_id: ProjectId,
|
||||
action: impl FnOnce(&project_repository::Repository, Option<&users::User>) -> Result<T>
|
||||
+ Send
|
||||
+ 'static,
|
||||
action: impl FnOnce(&project_repository::Repository) -> Result<T> + Send + 'static,
|
||||
) -> Result<JoinHandle<Result<T>>> {
|
||||
let project = self.projects.get(project_id)?;
|
||||
let project_repository = project_repository::Repository::open(&project)?;
|
||||
let user = self.users.get_user()?;
|
||||
super::integration::verify_branch(&project_repository)?;
|
||||
Ok(tokio::task::spawn_blocking(move || {
|
||||
action(&project_repository, user.as_ref())
|
||||
action(&project_repository)
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
@ -4,12 +4,11 @@ use super::*;
|
||||
async fn twice() {
|
||||
let data_dir = paths::data_dir();
|
||||
let projects = projects::Controller::from_path(data_dir.path());
|
||||
let users = users::Controller::from_path(data_dir.path());
|
||||
let helper = git::credentials::Helper::from_path(data_dir.path());
|
||||
|
||||
let test_project = TestProject::default();
|
||||
|
||||
let controller = Controller::new(projects.clone(), users, helper);
|
||||
let controller = Controller::new(projects.clone(), helper);
|
||||
|
||||
{
|
||||
let project = projects
|
||||
|
@ -5,7 +5,6 @@ use gitbutler_core::error::Marker;
|
||||
use gitbutler_core::{
|
||||
git,
|
||||
projects::{self, Project, ProjectId},
|
||||
users,
|
||||
virtual_branches::{branch, Controller},
|
||||
};
|
||||
use tempfile::TempDir;
|
||||
@ -33,7 +32,6 @@ impl Default for Test {
|
||||
fn default() -> Self {
|
||||
let data_dir = paths::data_dir();
|
||||
let projects = projects::Controller::from_path(data_dir.path());
|
||||
let users = users::Controller::from_path(data_dir.path());
|
||||
let helper = git::credentials::Helper::from_path(data_dir.path());
|
||||
|
||||
let test_project = TestProject::default();
|
||||
@ -44,7 +42,7 @@ impl Default for Test {
|
||||
Self {
|
||||
repository: test_project,
|
||||
project_id: project.id,
|
||||
controller: Controller::new(projects.clone(), users, helper),
|
||||
controller: Controller::new(projects.clone(), helper),
|
||||
projects,
|
||||
project,
|
||||
data_dir: Some(data_dir),
|
||||
|
@ -139,7 +139,6 @@ fn main() {
|
||||
|
||||
app_handle.manage(gitbutler_core::virtual_branches::controller::Controller::new(
|
||||
projects_controller.clone(),
|
||||
users_controller.clone(),
|
||||
git_credentials_controller.clone(),
|
||||
));
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user