From a7aa9b0e10adf4acc9cbcfeab52f7cc9c36a581d Mon Sep 17 00:00:00 2001 From: Kiril Videlov Date: Sun, 7 Jul 2024 20:48:12 +0200 Subject: [PATCH] move remote module to the branch crate --- crates/gitbutler-branch/src/assets.rs | 31 +++++++++++++++++-- crates/gitbutler-branch/src/base.rs | 7 +++-- crates/gitbutler-branch/src/controller.rs | 19 ++++++------ crates/gitbutler-branch/src/lib.rs | 2 ++ .../src}/remote.rs | 14 ++++----- crates/gitbutler-branch/src/virtual.rs | 3 +- crates/gitbutler-branch/tests/extra/mod.rs | 2 +- crates/gitbutler-core/src/assets.rs | 27 +--------------- .../src/virtual_branches/mod.rs | 3 -- .../gitbutler-tauri/src/virtual_branches.rs | 14 ++++----- 10 files changed, 61 insertions(+), 61 deletions(-) rename crates/{gitbutler-core/src/virtual_branches => gitbutler-branch/src}/remote.rs (94%) diff --git a/crates/gitbutler-branch/src/assets.rs b/crates/gitbutler-branch/src/assets.rs index e21c076f8..af72d98b1 100644 --- a/crates/gitbutler-branch/src/assets.rs +++ b/crates/gitbutler-branch/src/assets.rs @@ -1,6 +1,10 @@ use futures::future::join_all; -use crate::{base::BaseBranch, VirtualBranch, VirtualBranchCommit}; +use crate::{ + base::BaseBranch, + remote::{RemoteBranchData, RemoteCommit}, + VirtualBranch, VirtualBranchCommit, +}; #[derive(Clone)] pub struct Proxy { @@ -21,6 +25,27 @@ impl Proxy { .await } + pub async fn proxy_remote_branch_data(&self, branch: RemoteBranchData) -> RemoteBranchData { + RemoteBranchData { + commits: join_all( + branch + .commits + .into_iter() + .map(|commit| self.proxy_remote_commit(commit)) + .collect::>(), + ) + .await, + ..branch + } + } + + pub async fn proxy_remote_commit(&self, commit: RemoteCommit) -> RemoteCommit { + RemoteCommit { + author: self.core_proxy.proxy_author(commit.author).await, + ..commit + } + } + pub async fn proxy_virtual_branch(&self, branch: VirtualBranch) -> VirtualBranch { VirtualBranch { commits: join_all( @@ -52,7 +77,7 @@ impl Proxy { .clone() .recent_commits .into_iter() - .map(|commit| self.core_proxy.proxy_remote_commit(commit)) + .map(|commit| self.proxy_remote_commit(commit)) .collect::>(), ) .await, @@ -61,7 +86,7 @@ impl Proxy { .clone() .upstream_commits .into_iter() - .map(|commit| self.core_proxy.proxy_remote_commit(commit)) + .map(|commit| self.proxy_remote_commit(commit)) .collect::>(), ) .await, diff --git a/crates/gitbutler-branch/src/base.rs b/crates/gitbutler-branch/src/base.rs index 2ab84c2e3..80c82e512 100644 --- a/crates/gitbutler-branch/src/base.rs +++ b/crates/gitbutler-branch/src/base.rs @@ -7,9 +7,10 @@ use serde::Serialize; use super::r#virtual as vb; use super::r#virtual::convert_to_real_branch; use crate::integration::{get_workspace_head, update_gitbutler_integration}; +use crate::remote::{commit_to_remote_commit, RemoteCommit}; use crate::VirtualBranchHunk; use gitbutler_core::virtual_branches::{ - branch, target, BranchId, RemoteCommit, VirtualBranchesHandle, GITBUTLER_INTEGRATION_REFERENCE, + branch, target, BranchId, VirtualBranchesHandle, GITBUTLER_INTEGRATION_REFERENCE, }; use gitbutler_core::{error::Marker, git::RepositoryExt, rebase::cherry_rebase}; use gitbutler_core::{ @@ -588,7 +589,7 @@ pub fn target_to_base_branch( .log(oid, project_repository::LogUntil::Commit(target.sha)) .context("failed to get upstream commits")? .iter() - .map(gitbutler_core::virtual_branches::commit_to_remote_commit) + .map(commit_to_remote_commit) .collect::>(); // get some recent commits @@ -596,7 +597,7 @@ pub fn target_to_base_branch( .log(target.sha, LogUntil::Take(20)) .context("failed to get recent commits")? .iter() - .map(gitbutler_core::virtual_branches::commit_to_remote_commit) + .map(commit_to_remote_commit) .collect::>(); // there has got to be a better way to do this. diff --git a/crates/gitbutler-branch/src/controller.rs b/crates/gitbutler-branch/src/controller.rs index e5697c971..8a16b0b6d 100644 --- a/crates/gitbutler-branch/src/controller.rs +++ b/crates/gitbutler-branch/src/controller.rs @@ -14,8 +14,12 @@ use std::{path::Path, sync::Arc}; use tokio::sync::Semaphore; -use crate::base::{ - get_base_branch_data, set_base_branch, set_target_push_remote, update_base_branch, BaseBranch, +use crate::{ + base::{ + get_base_branch_data, set_base_branch, set_target_push_remote, update_base_branch, + BaseBranch, + }, + remote::{get_branch_data, list_remote_branches, RemoteBranch, RemoteBranchData}, }; use super::r#virtual as branch; @@ -388,21 +392,18 @@ impl Controller { branch::push(&project_repository, branch_id, with_force, &helper, askpass) } - pub async fn list_remote_branches( - &self, - project: Project, - ) -> Result> { + pub async fn list_remote_branches(&self, project: Project) -> Result> { let project_repository = Repository::open(&project)?; - virtual_branches::list_remote_branches(&project_repository) + list_remote_branches(&project_repository) } pub async fn get_remote_branch_data( &self, project: &Project, refname: &git::Refname, - ) -> Result { + ) -> Result { let project_repository = Repository::open(project)?; - virtual_branches::get_branch_data(&project_repository, refname) + get_branch_data(&project_repository, refname) } pub async fn squash( diff --git a/crates/gitbutler-branch/src/lib.rs b/crates/gitbutler-branch/src/lib.rs index 70f24e551..38932c12d 100644 --- a/crates/gitbutler-branch/src/lib.rs +++ b/crates/gitbutler-branch/src/lib.rs @@ -12,3 +12,5 @@ pub mod base; pub mod integration; pub mod files; + +pub mod remote; diff --git a/crates/gitbutler-core/src/virtual_branches/remote.rs b/crates/gitbutler-branch/src/remote.rs similarity index 94% rename from crates/gitbutler-core/src/virtual_branches/remote.rs rename to crates/gitbutler-branch/src/remote.rs index a95bb1d9f..77f51f960 100644 --- a/crates/gitbutler-core/src/virtual_branches/remote.rs +++ b/crates/gitbutler-branch/src/remote.rs @@ -4,8 +4,8 @@ use anyhow::{Context, Result}; use bstr::BString; use serde::Serialize; -use super::{target, Author, VirtualBranchesHandle}; -use crate::{ +use gitbutler_core::virtual_branches::{target, Author, VirtualBranchesHandle}; +use gitbutler_core::{ git::{self, CommitExt, RepositoryExt}, project_repository::{self, LogUntil}, }; @@ -22,7 +22,7 @@ use crate::{ #[derive(Debug, Clone, Serialize, PartialEq)] #[serde(rename_all = "camelCase")] pub struct RemoteBranch { - #[serde(with = "crate::serde::oid")] + #[serde(with = "gitbutler_core::serde::oid")] pub sha: git2::Oid, pub name: git::Refname, pub upstream: Option, @@ -33,13 +33,13 @@ pub struct RemoteBranch { #[derive(Debug, Clone, Serialize, PartialEq)] #[serde(rename_all = "camelCase")] pub struct RemoteBranchData { - #[serde(with = "crate::serde::oid")] + #[serde(with = "gitbutler_core::serde::oid")] pub sha: git2::Oid, pub name: git::Refname, pub upstream: Option, pub behind: u32, pub commits: Vec, - #[serde(with = "crate::serde::oid_opt", default)] + #[serde(with = "gitbutler_core::serde::oid_opt", default)] pub fork_point: Option, } @@ -47,12 +47,12 @@ pub struct RemoteBranchData { #[serde(rename_all = "camelCase")] pub struct RemoteCommit { pub id: String, - #[serde(serialize_with = "crate::serde::as_string_lossy")] + #[serde(serialize_with = "gitbutler_core::serde::as_string_lossy")] pub description: BString, pub created_at: u128, pub author: Author, pub change_id: Option, - #[serde(with = "crate::serde::oid_vec")] + #[serde(with = "gitbutler_core::serde::oid_vec")] pub parent_ids: Vec, } diff --git a/crates/gitbutler-branch/src/virtual.rs b/crates/gitbutler-branch/src/virtual.rs index 3f61bd3e4..76720fd6e 100644 --- a/crates/gitbutler-branch/src/virtual.rs +++ b/crates/gitbutler-branch/src/virtual.rs @@ -20,6 +20,7 @@ use hex::ToHex; use serde::{Deserialize, Serialize}; use crate::integration::{get_integration_commiter, get_workspace_head}; +use crate::remote::{branch_to_remote_branch, RemoteBranch}; use gitbutler_core::error::Code; use gitbutler_core::error::Marker; use gitbutler_core::git::diff::GitHunk; @@ -34,7 +35,7 @@ use gitbutler_core::virtual_branches::{ branch::{ self, Branch, BranchCreateRequest, BranchId, BranchOwnershipClaims, Hunk, OwnershipClaim, }, - branch_to_remote_branch, target, RemoteBranch, VirtualBranchesHandle, + target, VirtualBranchesHandle, }; use gitbutler_core::{ dedup::{dedup, dedup_fmt}, diff --git a/crates/gitbutler-branch/tests/extra/mod.rs b/crates/gitbutler-branch/tests/extra/mod.rs index c2e3e1941..774a9d369 100644 --- a/crates/gitbutler-branch/tests/extra/mod.rs +++ b/crates/gitbutler-branch/tests/extra/mod.rs @@ -1299,7 +1299,7 @@ fn detect_mergeable_branch() -> Result<()> { }; vb_state.set_branch(branch4.clone())?; - let remotes = gitbutler_core::virtual_branches::list_remote_branches(project_repository) + let remotes = gitbutler_branch::remote::list_remote_branches(project_repository) .expect("failed to list remotes"); let _remote1 = &remotes .iter() diff --git a/crates/gitbutler-core/src/assets.rs b/crates/gitbutler-core/src/assets.rs index 98b320062..67eff33fa 100644 --- a/crates/gitbutler-core/src/assets.rs +++ b/crates/gitbutler-core/src/assets.rs @@ -1,14 +1,10 @@ use std::{collections::HashMap, path, sync, time::Duration}; use anyhow::Result; -use futures::future::join_all; use tokio::sync::Semaphore; use url::Url; -use crate::{ - users, - virtual_branches::{Author, RemoteBranchData, RemoteCommit}, -}; +use crate::{users, virtual_branches::Author}; #[derive(Clone)] pub struct Proxy { @@ -38,20 +34,6 @@ impl Proxy { user } - pub async fn proxy_remote_branch_data(&self, branch: RemoteBranchData) -> RemoteBranchData { - RemoteBranchData { - commits: join_all( - branch - .commits - .into_iter() - .map(|commit| self.proxy_remote_commit(commit)) - .collect::>(), - ) - .await, - ..branch - } - } - pub async fn proxy_author(&self, author: Author) -> Author { Author { gravatar_url: self.proxy(&author.gravatar_url).await.unwrap_or_else(|error| { @@ -62,13 +44,6 @@ impl Proxy { } } - pub async fn proxy_remote_commit(&self, commit: RemoteCommit) -> RemoteCommit { - RemoteCommit { - author: self.proxy_author(commit.author).await, - ..commit - } - } - // takes a url of a remote assets, downloads it into cache and returns a url that points to the cached file pub async fn proxy(&self, src: &Url) -> Result { #[cfg(unix)] diff --git a/crates/gitbutler-core/src/virtual_branches/mod.rs b/crates/gitbutler-core/src/virtual_branches/mod.rs index 456ec804e..221a88d2c 100644 --- a/crates/gitbutler-core/src/virtual_branches/mod.rs +++ b/crates/gitbutler-core/src/virtual_branches/mod.rs @@ -2,9 +2,6 @@ pub mod branch; pub use branch::{Branch, BranchId}; pub mod target; -mod remote; -pub use remote::*; - mod state; pub use state::VirtualBranches as VirtualBranchesState; pub use state::VirtualBranchesHandle; diff --git a/crates/gitbutler-tauri/src/virtual_branches.rs b/crates/gitbutler-tauri/src/virtual_branches.rs index e0167b694..0ed74a031 100644 --- a/crates/gitbutler-tauri/src/virtual_branches.rs +++ b/crates/gitbutler-tauri/src/virtual_branches.rs @@ -3,6 +3,7 @@ pub mod commands { use anyhow::{anyhow, Context}; use gitbutler_branch::base::BaseBranch; use gitbutler_branch::files::RemoteBranchFile; + use gitbutler_branch::remote::{RemoteBranch, RemoteBranchData}; use gitbutler_branch::{Controller, NameConflitResolution, VirtualBranches}; use gitbutler_core::{ assets, @@ -10,10 +11,7 @@ pub mod commands { git, projects::{self, ProjectId}, types::ReferenceName, - virtual_branches::{ - branch::{self, BranchId, BranchOwnershipClaims}, - RemoteBranch, RemoteBranchData, - }, + virtual_branches::branch::{self, BranchId, BranchOwnershipClaims}, }; use tauri::{AppHandle, Manager}; use tracing::instrument; @@ -457,10 +455,10 @@ pub mod commands { .state::() .get_remote_branch_data(&project, &refname) .await?; - let branch_data = handle - .state::() - .proxy_remote_branch_data(branch_data) - .await; + + let proxy = + gitbutler_branch::assets::Proxy::new(handle.state::().inner().clone()); + let branch_data = proxy.proxy_remote_branch_data(branch_data).await; Ok(branch_data) }