From 71809fc882ea35f307cfa7c19e58275a18e19342 Mon Sep 17 00:00:00 2001 From: Kiril Videlov Date: Sun, 7 Jul 2024 21:10:31 +0200 Subject: [PATCH 1/4] remove project dependency on oplog --- crates/gitbutler-branch/src/base.rs | 3 ++- crates/gitbutler-branch/src/controller.rs | 1 + crates/gitbutler-branch/src/integration.rs | 2 +- crates/gitbutler-branch/src/virtual.rs | 2 +- crates/gitbutler-branch/tests/extra/mod.rs | 5 ++++- .../gitbutler-branch/tests/virtual_branches/oplog.rs | 1 + crates/gitbutler-core/src/projects/project.rs | 7 +------ crates/gitbutler-core/src/virtual_branches/mod.rs | 1 + crates/gitbutler-core/src/virtual_branches/state.rs | 12 +++++++++++- .../tests/virtual_branches/iterator.rs | 2 +- crates/gitbutler-oplog/src/oplog.rs | 3 ++- crates/gitbutler-sync/src/cloud.rs | 1 + crates/gitbutler-testsupport/src/lib.rs | 5 ++++- 13 files changed, 31 insertions(+), 14 deletions(-) diff --git a/crates/gitbutler-branch/src/base.rs b/crates/gitbutler-branch/src/base.rs index 80c82e512..52102cce2 100644 --- a/crates/gitbutler-branch/src/base.rs +++ b/crates/gitbutler-branch/src/base.rs @@ -10,7 +10,8 @@ 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, VirtualBranchesHandle, GITBUTLER_INTEGRATION_REFERENCE, + branch, target, BranchId, VirtualBranchesAccess, VirtualBranchesHandle, + GITBUTLER_INTEGRATION_REFERENCE, }; use gitbutler_core::{error::Marker, git::RepositoryExt, rebase::cherry_rebase}; use gitbutler_core::{ diff --git a/crates/gitbutler-branch/src/controller.rs b/crates/gitbutler-branch/src/controller.rs index 8a16b0b6d..4084b7189 100644 --- a/crates/gitbutler-branch/src/controller.rs +++ b/crates/gitbutler-branch/src/controller.rs @@ -4,6 +4,7 @@ use gitbutler_core::{ project_repository::Repository, projects::FetchResult, types::ReferenceName, + virtual_branches::VirtualBranchesAccess, }; use gitbutler_oplog::{ entry::{OperationKind, SnapshotDetails}, diff --git a/crates/gitbutler-branch/src/integration.rs b/crates/gitbutler-branch/src/integration.rs index e7364dbed..c4f7d8ab3 100644 --- a/crates/gitbutler-branch/src/integration.rs +++ b/crates/gitbutler-branch/src/integration.rs @@ -6,7 +6,7 @@ use bstr::ByteSlice; use gitbutler_core::error::Marker; use gitbutler_core::git::RepositoryExt; use gitbutler_core::virtual_branches::{ - VirtualBranchesHandle, GITBUTLER_INTEGRATION_COMMIT_AUTHOR_EMAIL, + VirtualBranchesAccess, VirtualBranchesHandle, GITBUTLER_INTEGRATION_COMMIT_AUTHOR_EMAIL, GITBUTLER_INTEGRATION_COMMIT_AUTHOR_NAME, GITBUTLER_INTEGRATION_REFERENCE, }; use gitbutler_core::{ diff --git a/crates/gitbutler-branch/src/virtual.rs b/crates/gitbutler-branch/src/virtual.rs index 76720fd6e..d16f54550 100644 --- a/crates/gitbutler-branch/src/virtual.rs +++ b/crates/gitbutler-branch/src/virtual.rs @@ -15,7 +15,7 @@ use diffy::{apply_bytes as diffy_apply, Line, Patch}; use git2::build::TreeUpdateBuilder; use git2::ErrorCode; use git2_hooks::HookResult; -use gitbutler_core::virtual_branches::Author; +use gitbutler_core::virtual_branches::{Author, VirtualBranchesAccess}; use hex::ToHex; use serde::{Deserialize, Serialize}; diff --git a/crates/gitbutler-branch/tests/extra/mod.rs b/crates/gitbutler-branch/tests/extra/mod.rs index 774a9d369..afa371840 100644 --- a/crates/gitbutler-branch/tests/extra/mod.rs +++ b/crates/gitbutler-branch/tests/extra/mod.rs @@ -19,7 +19,10 @@ use gitbutler_branch::r#virtual::{ }; use gitbutler_core::{ git::{self, CommitExt, RepositoryExt}, - virtual_branches::branch::{BranchCreateRequest, BranchOwnershipClaims, BranchUpdateRequest}, + virtual_branches::{ + branch::{BranchCreateRequest, BranchOwnershipClaims, BranchUpdateRequest}, + VirtualBranchesAccess, + }, }; use pretty_assertions::assert_eq; diff --git a/crates/gitbutler-branch/tests/virtual_branches/oplog.rs b/crates/gitbutler-branch/tests/virtual_branches/oplog.rs index 483ab36c9..e414eea54 100644 --- a/crates/gitbutler-branch/tests/virtual_branches/oplog.rs +++ b/crates/gitbutler-branch/tests/virtual_branches/oplog.rs @@ -1,4 +1,5 @@ use super::*; +use gitbutler_core::virtual_branches::VirtualBranchesAccess; use gitbutler_oplog::oplog::Oplog; use itertools::Itertools; use std::io::Write; diff --git a/crates/gitbutler-core/src/projects/project.rs b/crates/gitbutler-core/src/projects/project.rs index 8493ea527..6fba7acea 100644 --- a/crates/gitbutler-core/src/projects/project.rs +++ b/crates/gitbutler-core/src/projects/project.rs @@ -5,7 +5,7 @@ use std::{ use serde::{Deserialize, Serialize}; -use crate::{id::Id, types::default_true::DefaultTrue, virtual_branches::VirtualBranchesHandle}; +use crate::{id::Id, types::default_true::DefaultTrue}; #[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)] #[serde(rename_all = "camelCase")] @@ -117,11 +117,6 @@ impl Project { self.path.join(".git").join("gitbutler") } - /// Returns a handle to the virtual branches manager of the project. - pub fn virtual_branches(&self) -> VirtualBranchesHandle { - VirtualBranchesHandle::new(self.gb_dir()) - } - pub fn snapshot_lines_threshold(&self) -> usize { self.snapshot_lines_threshold.unwrap_or(20) } diff --git a/crates/gitbutler-core/src/virtual_branches/mod.rs b/crates/gitbutler-core/src/virtual_branches/mod.rs index 221a88d2c..5cf3ddec0 100644 --- a/crates/gitbutler-core/src/virtual_branches/mod.rs +++ b/crates/gitbutler-core/src/virtual_branches/mod.rs @@ -4,6 +4,7 @@ pub mod target; mod state; pub use state::VirtualBranches as VirtualBranchesState; +pub use state::VirtualBranchesAccess; pub use state::VirtualBranchesHandle; mod author; diff --git a/crates/gitbutler-core/src/virtual_branches/state.rs b/crates/gitbutler-core/src/virtual_branches/state.rs index f107aee13..958f17bd0 100644 --- a/crates/gitbutler-core/src/virtual_branches/state.rs +++ b/crates/gitbutler-core/src/virtual_branches/state.rs @@ -3,7 +3,7 @@ use std::{ path::{Path, PathBuf}, }; -use crate::{error::Code, fs::read_toml_file_or_default}; +use crate::{error::Code, fs::read_toml_file_or_default, projects::Project}; use anyhow::{anyhow, Result}; use itertools::Itertools; use serde::{Deserialize, Serialize}; @@ -29,6 +29,16 @@ pub struct VirtualBranchesHandle { file_path: PathBuf, } +pub trait VirtualBranchesAccess { + fn virtual_branches(&self) -> VirtualBranchesHandle; +} + +impl VirtualBranchesAccess for Project { + fn virtual_branches(&self) -> VirtualBranchesHandle { + VirtualBranchesHandle::new(self.gb_dir()) + } +} + impl VirtualBranchesHandle { /// Creates a new concurrency-safe handle to the state of virtual branches. pub fn new>(base_path: P) -> Self { diff --git a/crates/gitbutler-core/tests/virtual_branches/iterator.rs b/crates/gitbutler-core/tests/virtual_branches/iterator.rs index 70d276b81..3d51ad017 100644 --- a/crates/gitbutler-core/tests/virtual_branches/iterator.rs +++ b/crates/gitbutler-core/tests/virtual_branches/iterator.rs @@ -1,7 +1,7 @@ use std::sync::atomic::{AtomicUsize, Ordering}; use anyhow::Result; -use gitbutler_core::virtual_branches; +use gitbutler_core::virtual_branches::{self, VirtualBranchesAccess}; use once_cell::sync::Lazy; use gitbutler_testsupport::{Case, Suite}; diff --git a/crates/gitbutler-oplog/src/oplog.rs b/crates/gitbutler-oplog/src/oplog.rs index f5dfd855e..efa826e57 100644 --- a/crates/gitbutler-oplog/src/oplog.rs +++ b/crates/gitbutler-oplog/src/oplog.rs @@ -11,7 +11,8 @@ use tracing::instrument; use gitbutler_core::git::diff::FileDiff; use gitbutler_core::virtual_branches::{ - Branch, GITBUTLER_INTEGRATION_COMMIT_AUTHOR_EMAIL, GITBUTLER_INTEGRATION_COMMIT_AUTHOR_NAME, + Branch, VirtualBranchesAccess, GITBUTLER_INTEGRATION_COMMIT_AUTHOR_EMAIL, + GITBUTLER_INTEGRATION_COMMIT_AUTHOR_NAME, }; use gitbutler_core::{git::diff::hunks_by_filepath, git::RepositoryExt, projects::Project}; diff --git a/crates/gitbutler-sync/src/cloud.rs b/crates/gitbutler-sync/src/cloud.rs index 70a695fd5..6f28762bf 100644 --- a/crates/gitbutler-sync/src/cloud.rs +++ b/crates/gitbutler-sync/src/cloud.rs @@ -2,6 +2,7 @@ use std::time; use anyhow::{Context, Result}; use gitbutler_core::id::Id; +use gitbutler_core::virtual_branches::VirtualBranchesAccess; use gitbutler_core::{ git::{self}, project_repository, diff --git a/crates/gitbutler-testsupport/src/lib.rs b/crates/gitbutler-testsupport/src/lib.rs index 6595196e6..8382e217c 100644 --- a/crates/gitbutler-testsupport/src/lib.rs +++ b/crates/gitbutler-testsupport/src/lib.rs @@ -18,7 +18,10 @@ pub mod paths { } pub mod virtual_branches { - use gitbutler_core::{project_repository, virtual_branches}; + use gitbutler_core::{ + project_repository, + virtual_branches::{self, VirtualBranchesAccess}, + }; use crate::empty_bare_repository; From 00a77d93275124c1c70d70f322c2c2b7ee6d4579 Mon Sep 17 00:00:00 2001 From: Kiril Videlov Date: Sun, 7 Jul 2024 21:26:07 +0200 Subject: [PATCH 2/4] move branch state into a separate crate --- Cargo.lock | 15 +++++++++++++++ Cargo.toml | 4 +++- crates/gitbutler-branch/Cargo.toml | 1 + crates/gitbutler-branch/src/base.rs | 6 ++---- crates/gitbutler-branch/src/controller.rs | 4 ++-- crates/gitbutler-branch/src/integration.rs | 5 +++-- crates/gitbutler-branch/src/remote.rs | 3 ++- crates/gitbutler-branch/src/virtual.rs | 5 +++-- crates/gitbutler-branch/tests/extra/mod.rs | 6 ++---- .../tests/virtual_branches/oplog.rs | 2 +- crates/gitbutler-branchstate/Cargo.toml | 13 +++++++++++++ crates/gitbutler-branchstate/src/lib.rs | 4 ++++ .../src}/state.rs | 7 +++---- crates/gitbutler-core/src/virtual_branches/mod.rs | 5 ----- crates/gitbutler-oplog/Cargo.toml | 1 + crates/gitbutler-oplog/src/oplog.rs | 7 +++---- crates/gitbutler-sync/Cargo.toml | 1 + crates/gitbutler-sync/src/cloud.rs | 2 +- crates/gitbutler-testsupport/Cargo.toml | 1 + crates/gitbutler-testsupport/src/lib.rs | 6 ++---- 20 files changed, 63 insertions(+), 35 deletions(-) create mode 100644 crates/gitbutler-branchstate/Cargo.toml create mode 100644 crates/gitbutler-branchstate/src/lib.rs rename crates/{gitbutler-core/src/virtual_branches => gitbutler-branchstate/src}/state.rs (95%) diff --git a/Cargo.lock b/Cargo.lock index 49b6da752..f6708fb5d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2112,6 +2112,7 @@ dependencies = [ "futures", "git2", "git2-hooks", + "gitbutler-branchstate", "gitbutler-core", "gitbutler-git", "gitbutler-oplog", @@ -2131,6 +2132,17 @@ dependencies = [ "url", ] +[[package]] +name = "gitbutler-branchstate" +version = "0.0.0" +dependencies = [ + "anyhow", + "gitbutler-core", + "itertools 0.13.0", + "serde", + "toml 0.8.14", +] + [[package]] name = "gitbutler-cli" version = "0.0.0" @@ -2234,6 +2246,7 @@ version = "0.0.0" dependencies = [ "anyhow", "git2", + "gitbutler-branchstate", "gitbutler-core", "gix", "itertools 0.13.0", @@ -2251,6 +2264,7 @@ version = "0.0.0" dependencies = [ "anyhow", "git2", + "gitbutler-branchstate", "gitbutler-core", "gitbutler-oplog", "itertools 0.13.0", @@ -2303,6 +2317,7 @@ dependencies = [ "anyhow", "git2", "gitbutler-branch", + "gitbutler-branchstate", "gitbutler-core", "keyring", "once_cell", diff --git a/Cargo.toml b/Cargo.toml index ae3ceaa66..ab7b2e577 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,8 @@ members = [ "crates/gitbutler-cli", "crates/gitbutler-branch", "crates/gitbutler-sync", - "crates/gitbutler-oplog", + "crates/gitbutler-oplog", + "crates/gitbutler-branchstate", ] resolver = "2" @@ -31,6 +32,7 @@ gitbutler-cli ={ path = "crates/gitbutler-cli" } gitbutler-branch = { path = "crates/gitbutler-branch" } gitbutler-sync = { path = "crates/gitbutler-sync" } gitbutler-oplog = { path = "crates/gitbutler-oplog" } +gitbutler-branchstate = { path = "crates/gitbutler-branchstate" } [profile.release] codegen-units = 1 # Compile crates one after another so the compiler can optimize better diff --git a/crates/gitbutler-branch/Cargo.toml b/crates/gitbutler-branch/Cargo.toml index 911979e19..e2273062c 100644 --- a/crates/gitbutler-branch/Cargo.toml +++ b/crates/gitbutler-branch/Cargo.toml @@ -12,6 +12,7 @@ git2.workspace = true tokio.workspace = true gitbutler-core.workspace = true gitbutler-oplog.workspace = true +gitbutler-branchstate.workspace = true serde = { workspace = true, features = ["std"]} bstr = "1.9.1" diffy = "0.3.0" diff --git a/crates/gitbutler-branch/src/base.rs b/crates/gitbutler-branch/src/base.rs index 52102cce2..c82bac519 100644 --- a/crates/gitbutler-branch/src/base.rs +++ b/crates/gitbutler-branch/src/base.rs @@ -2,6 +2,7 @@ use std::{path::Path, time}; use anyhow::{anyhow, Context, Result}; use git2::Index; +use gitbutler_branchstate::{VirtualBranchesAccess, VirtualBranchesHandle}; use serde::Serialize; use super::r#virtual as vb; @@ -9,10 +10,7 @@ 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, VirtualBranchesAccess, VirtualBranchesHandle, - GITBUTLER_INTEGRATION_REFERENCE, -}; +use gitbutler_core::virtual_branches::{branch, target, BranchId, GITBUTLER_INTEGRATION_REFERENCE}; use gitbutler_core::{error::Marker, git::RepositoryExt, rebase::cherry_rebase}; use gitbutler_core::{ git::{self, diff}, diff --git a/crates/gitbutler-branch/src/controller.rs b/crates/gitbutler-branch/src/controller.rs index 4084b7189..20447f34c 100644 --- a/crates/gitbutler-branch/src/controller.rs +++ b/crates/gitbutler-branch/src/controller.rs @@ -1,10 +1,10 @@ use anyhow::Result; +use gitbutler_branchstate::{VirtualBranchesAccess, VirtualBranchesHandle}; use gitbutler_core::{ git::{credentials::Helper, BranchExt}, project_repository::Repository, projects::FetchResult, types::ReferenceName, - virtual_branches::VirtualBranchesAccess, }; use gitbutler_oplog::{ entry::{OperationKind, SnapshotDetails}, @@ -29,7 +29,7 @@ use gitbutler_core::virtual_branches; use crate::files::RemoteBranchFile; use gitbutler_core::virtual_branches::{ branch::{BranchId, BranchOwnershipClaims}, - target, VirtualBranchesHandle, + target, }; use gitbutler_core::{ git, diff --git a/crates/gitbutler-branch/src/integration.rs b/crates/gitbutler-branch/src/integration.rs index c4f7d8ab3..ef11dd5e0 100644 --- a/crates/gitbutler-branch/src/integration.rs +++ b/crates/gitbutler-branch/src/integration.rs @@ -3,11 +3,12 @@ use std::{path::PathBuf, vec}; use anyhow::{anyhow, bail, Context, Result}; use bstr::ByteSlice; +use gitbutler_branchstate::{VirtualBranchesAccess, VirtualBranchesHandle}; use gitbutler_core::error::Marker; use gitbutler_core::git::RepositoryExt; use gitbutler_core::virtual_branches::{ - VirtualBranchesAccess, VirtualBranchesHandle, GITBUTLER_INTEGRATION_COMMIT_AUTHOR_EMAIL, - GITBUTLER_INTEGRATION_COMMIT_AUTHOR_NAME, GITBUTLER_INTEGRATION_REFERENCE, + GITBUTLER_INTEGRATION_COMMIT_AUTHOR_EMAIL, GITBUTLER_INTEGRATION_COMMIT_AUTHOR_NAME, + GITBUTLER_INTEGRATION_REFERENCE, }; use gitbutler_core::{ git::CommitExt, diff --git a/crates/gitbutler-branch/src/remote.rs b/crates/gitbutler-branch/src/remote.rs index 77f51f960..dc4169b84 100644 --- a/crates/gitbutler-branch/src/remote.rs +++ b/crates/gitbutler-branch/src/remote.rs @@ -2,9 +2,10 @@ use std::path::Path; use anyhow::{Context, Result}; use bstr::BString; +use gitbutler_branchstate::VirtualBranchesHandle; use serde::Serialize; -use gitbutler_core::virtual_branches::{target, Author, VirtualBranchesHandle}; +use gitbutler_core::virtual_branches::{target, Author}; use gitbutler_core::{ git::{self, CommitExt, RepositoryExt}, project_repository::{self, LogUntil}, diff --git a/crates/gitbutler-branch/src/virtual.rs b/crates/gitbutler-branch/src/virtual.rs index d16f54550..e62134e82 100644 --- a/crates/gitbutler-branch/src/virtual.rs +++ b/crates/gitbutler-branch/src/virtual.rs @@ -1,3 +1,4 @@ +use gitbutler_branchstate::{VirtualBranchesAccess, VirtualBranchesHandle}; use gitbutler_oplog::snapshot::Snapshot; use std::borrow::Borrow; #[cfg(target_family = "unix")] @@ -15,7 +16,7 @@ use diffy::{apply_bytes as diffy_apply, Line, Patch}; use git2::build::TreeUpdateBuilder; use git2::ErrorCode; use git2_hooks::HookResult; -use gitbutler_core::virtual_branches::{Author, VirtualBranchesAccess}; +use gitbutler_core::virtual_branches::Author; use hex::ToHex; use serde::{Deserialize, Serialize}; @@ -35,7 +36,7 @@ use gitbutler_core::virtual_branches::{ branch::{ self, Branch, BranchCreateRequest, BranchId, BranchOwnershipClaims, Hunk, OwnershipClaim, }, - target, VirtualBranchesHandle, + target, }; 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 afa371840..09dba73b1 100644 --- a/crates/gitbutler-branch/tests/extra/mod.rs +++ b/crates/gitbutler-branch/tests/extra/mod.rs @@ -17,12 +17,10 @@ use gitbutler_branch::r#virtual::{ commit, create_virtual_branch, create_virtual_branch_from_branch, integrate_upstream_commits, is_remote_branch_mergeable, list_virtual_branches, unapply_ownership, update_branch, }; +use gitbutler_branchstate::VirtualBranchesAccess; use gitbutler_core::{ git::{self, CommitExt, RepositoryExt}, - virtual_branches::{ - branch::{BranchCreateRequest, BranchOwnershipClaims, BranchUpdateRequest}, - VirtualBranchesAccess, - }, + virtual_branches::branch::{BranchCreateRequest, BranchOwnershipClaims, BranchUpdateRequest}, }; use pretty_assertions::assert_eq; diff --git a/crates/gitbutler-branch/tests/virtual_branches/oplog.rs b/crates/gitbutler-branch/tests/virtual_branches/oplog.rs index e414eea54..e2e837782 100644 --- a/crates/gitbutler-branch/tests/virtual_branches/oplog.rs +++ b/crates/gitbutler-branch/tests/virtual_branches/oplog.rs @@ -1,5 +1,5 @@ use super::*; -use gitbutler_core::virtual_branches::VirtualBranchesAccess; +use gitbutler_branchstate::VirtualBranchesAccess; use gitbutler_oplog::oplog::Oplog; use itertools::Itertools; use std::io::Write; diff --git a/crates/gitbutler-branchstate/Cargo.toml b/crates/gitbutler-branchstate/Cargo.toml new file mode 100644 index 000000000..747e5116c --- /dev/null +++ b/crates/gitbutler-branchstate/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "gitbutler-branchstate" +version = "0.0.0" +edition = "2021" +authors = ["GitButler "] +publish = false + +[dependencies] +gitbutler-core.workspace = true +itertools = "0.13" +anyhow = "1.0.86" +toml = "0.8.13" +serde = { workspace = true, features = ["std"]} diff --git a/crates/gitbutler-branchstate/src/lib.rs b/crates/gitbutler-branchstate/src/lib.rs new file mode 100644 index 000000000..9f01daa2b --- /dev/null +++ b/crates/gitbutler-branchstate/src/lib.rs @@ -0,0 +1,4 @@ +mod state; +pub use state::VirtualBranches as VirtualBranchesState; +pub use state::VirtualBranchesAccess; +pub use state::VirtualBranchesHandle; diff --git a/crates/gitbutler-core/src/virtual_branches/state.rs b/crates/gitbutler-branchstate/src/state.rs similarity index 95% rename from crates/gitbutler-core/src/virtual_branches/state.rs rename to crates/gitbutler-branchstate/src/state.rs index 958f17bd0..98a465486 100644 --- a/crates/gitbutler-core/src/virtual_branches/state.rs +++ b/crates/gitbutler-branchstate/src/state.rs @@ -3,13 +3,12 @@ use std::{ path::{Path, PathBuf}, }; -use crate::{error::Code, fs::read_toml_file_or_default, projects::Project}; use anyhow::{anyhow, Result}; +use gitbutler_core::{error::Code, fs::read_toml_file_or_default, projects::Project}; use itertools::Itertools; use serde::{Deserialize, Serialize}; -use super::{target::Target, Branch}; -use crate::virtual_branches::BranchId; +use gitbutler_core::virtual_branches::{target::Target, Branch, BranchId}; /// The state of virtual branches data, as persisted in a TOML file. #[derive(Serialize, Deserialize, Debug, Default)] @@ -172,5 +171,5 @@ impl VirtualBranchesHandle { } fn write>(file_path: P, virtual_branches: &VirtualBranches) -> Result<()> { - crate::fs::write(file_path, toml::to_string(&virtual_branches)?) + gitbutler_core::fs::write(file_path, toml::to_string(&virtual_branches)?) } diff --git a/crates/gitbutler-core/src/virtual_branches/mod.rs b/crates/gitbutler-core/src/virtual_branches/mod.rs index 5cf3ddec0..45d884d67 100644 --- a/crates/gitbutler-core/src/virtual_branches/mod.rs +++ b/crates/gitbutler-core/src/virtual_branches/mod.rs @@ -2,11 +2,6 @@ pub mod branch; pub use branch::{Branch, BranchId}; pub mod target; -mod state; -pub use state::VirtualBranches as VirtualBranchesState; -pub use state::VirtualBranchesAccess; -pub use state::VirtualBranchesHandle; - mod author; pub use author::Author; diff --git a/crates/gitbutler-oplog/Cargo.toml b/crates/gitbutler-oplog/Cargo.toml index 4b2eb7b1d..0fe179b28 100644 --- a/crates/gitbutler-oplog/Cargo.toml +++ b/crates/gitbutler-oplog/Cargo.toml @@ -9,6 +9,7 @@ publish = false anyhow = "1.0.86" git2.workspace = true gitbutler-core.workspace = true +gitbutler-branchstate.workspace = true serde = { workspace = true, features = ["std"]} itertools = "0.13" strum = { version = "0.26", features = ["derive"] } diff --git a/crates/gitbutler-oplog/src/oplog.rs b/crates/gitbutler-oplog/src/oplog.rs index efa826e57..c6b618c88 100644 --- a/crates/gitbutler-oplog/src/oplog.rs +++ b/crates/gitbutler-oplog/src/oplog.rs @@ -1,5 +1,6 @@ use anyhow::{anyhow, bail, Context}; use git2::{DiffOptions, FileMode}; +use gitbutler_branchstate::{VirtualBranchesAccess, VirtualBranchesState}; use std::collections::HashMap; use std::path::Path; use std::str::{from_utf8, FromStr}; @@ -11,8 +12,7 @@ use tracing::instrument; use gitbutler_core::git::diff::FileDiff; use gitbutler_core::virtual_branches::{ - Branch, VirtualBranchesAccess, GITBUTLER_INTEGRATION_COMMIT_AUTHOR_EMAIL, - GITBUTLER_INTEGRATION_COMMIT_AUTHOR_NAME, + Branch, GITBUTLER_INTEGRATION_COMMIT_AUTHOR_EMAIL, GITBUTLER_INTEGRATION_COMMIT_AUTHOR_NAME, }; use gitbutler_core::{git::diff::hunks_by_filepath, git::RepositoryExt, projects::Project}; @@ -808,8 +808,7 @@ fn tree_from_applied_vbranches( .find_blob(vb_toml_entry.id()) .context("failed to convert virtual_branches tree entry to blob")?; - let vbs_from_toml: gitbutler_core::virtual_branches::VirtualBranchesState = - toml::from_str(from_utf8(vb_toml_blob.content())?)?; + let vbs_from_toml: VirtualBranchesState = toml::from_str(from_utf8(vb_toml_blob.content())?)?; let applied_branch_trees: Vec = vbs_from_toml .branches .values() diff --git a/crates/gitbutler-sync/Cargo.toml b/crates/gitbutler-sync/Cargo.toml index baee0c00f..04d5895ab 100644 --- a/crates/gitbutler-sync/Cargo.toml +++ b/crates/gitbutler-sync/Cargo.toml @@ -12,3 +12,4 @@ itertools = "0.13" git2.workspace = true gitbutler-core.workspace = true gitbutler-oplog.workspace = true +gitbutler-branchstate.workspace = true diff --git a/crates/gitbutler-sync/src/cloud.rs b/crates/gitbutler-sync/src/cloud.rs index 6f28762bf..d22aed336 100644 --- a/crates/gitbutler-sync/src/cloud.rs +++ b/crates/gitbutler-sync/src/cloud.rs @@ -1,8 +1,8 @@ use std::time; use anyhow::{Context, Result}; +use gitbutler_branchstate::VirtualBranchesAccess; use gitbutler_core::id::Id; -use gitbutler_core::virtual_branches::VirtualBranchesAccess; use gitbutler_core::{ git::{self}, project_repository, diff --git a/crates/gitbutler-testsupport/Cargo.toml b/crates/gitbutler-testsupport/Cargo.toml index 03e9ab835..61df626b1 100644 --- a/crates/gitbutler-testsupport/Cargo.toml +++ b/crates/gitbutler-testsupport/Cargo.toml @@ -19,3 +19,4 @@ keyring.workspace = true serde_json = "1.0" gitbutler-core = { path = "../gitbutler-core" } gitbutler-branch = { path = "../gitbutler-branch" } +gitbutler-branchstate = { path = "../gitbutler-branchstate" } diff --git a/crates/gitbutler-testsupport/src/lib.rs b/crates/gitbutler-testsupport/src/lib.rs index 8382e217c..9b685dec5 100644 --- a/crates/gitbutler-testsupport/src/lib.rs +++ b/crates/gitbutler-testsupport/src/lib.rs @@ -18,10 +18,8 @@ pub mod paths { } pub mod virtual_branches { - use gitbutler_core::{ - project_repository, - virtual_branches::{self, VirtualBranchesAccess}, - }; + use gitbutler_branchstate::VirtualBranchesAccess; + use gitbutler_core::{project_repository, virtual_branches}; use crate::empty_bare_repository; From e2bfb03fa92fe0ae84b0bfdd620322e767aa9b31 Mon Sep 17 00:00:00 2001 From: Kiril Videlov Date: Sun, 7 Jul 2024 21:27:09 +0200 Subject: [PATCH 3/4] remove irrelevant test This test used to exercise the custom iterator that existed before --- .../tests/virtual_branches/iterator.rs | 105 ------------------ .../tests/virtual_branches/mod.rs | 1 - 2 files changed, 106 deletions(-) delete mode 100644 crates/gitbutler-core/tests/virtual_branches/iterator.rs diff --git a/crates/gitbutler-core/tests/virtual_branches/iterator.rs b/crates/gitbutler-core/tests/virtual_branches/iterator.rs deleted file mode 100644 index 3d51ad017..000000000 --- a/crates/gitbutler-core/tests/virtual_branches/iterator.rs +++ /dev/null @@ -1,105 +0,0 @@ -use std::sync::atomic::{AtomicUsize, Ordering}; - -use anyhow::Result; -use gitbutler_core::virtual_branches::{self, VirtualBranchesAccess}; -use once_cell::sync::Lazy; - -use gitbutler_testsupport::{Case, Suite}; - -static TEST_INDEX: Lazy = Lazy::new(|| AtomicUsize::new(0)); - -fn new_test_branch() -> virtual_branches::branch::Branch { - TEST_INDEX.fetch_add(1, Ordering::Relaxed); - - virtual_branches::branch::Branch { - id: virtual_branches::BranchId::generate(), - name: format!("branch_name_{}", TEST_INDEX.load(Ordering::Relaxed)), - notes: String::new(), - applied: true, - upstream: Some( - format!( - "refs/remotes/origin/upstream_{}", - TEST_INDEX.load(Ordering::Relaxed) - ) - .parse() - .unwrap(), - ), - upstream_head: None, - created_timestamp_ms: TEST_INDEX.load(Ordering::Relaxed) as u128, - updated_timestamp_ms: (TEST_INDEX.load(Ordering::Relaxed) + 100) as u128, - head: format!( - "0123456789abcdef0123456789abcdef0123456{}", - TEST_INDEX.load(Ordering::Relaxed) - ) - .parse() - .unwrap(), - tree: format!( - "0123456789abcdef0123456789abcdef012345{}", - TEST_INDEX.load(Ordering::Relaxed) + 10 - ) - .parse() - .unwrap(), - ownership: virtual_branches::branch::BranchOwnershipClaims::default(), - order: TEST_INDEX.load(Ordering::Relaxed), - selected_for_changes: Some(1), - allow_rebasing: true, - } -} - -static TEST_TARGET_INDEX: Lazy = Lazy::new(|| AtomicUsize::new(0)); - -fn new_test_target() -> virtual_branches::target::Target { - virtual_branches::target::Target { - branch: format!( - "refs/remotes/branch name{}/remote name {}", - TEST_TARGET_INDEX.load(Ordering::Relaxed), - TEST_TARGET_INDEX.load(Ordering::Relaxed) - ) - .parse() - .unwrap(), - remote_url: format!("remote url {}", TEST_TARGET_INDEX.load(Ordering::Relaxed)), - sha: format!( - "0123456789abcdef0123456789abcdef0123456{}", - TEST_TARGET_INDEX.load(Ordering::Relaxed) - ) - .parse() - .unwrap(), - push_remote_name: None, - } -} - -#[test] -fn empty_iterator() -> Result<()> { - let suite = Suite::default(); - let Case { project, .. } = &suite.new_case(); - - let vb_state = project.virtual_branches(); - let iter = vb_state.list_branches()?; - - assert_eq!(iter.len(), 0); - - Ok(()) -} - -#[test] -fn iterate_all() -> Result<()> { - let suite = Suite::default(); - let Case { project, .. } = &suite.new_case(); - - let vb_state = project.virtual_branches(); - vb_state.set_default_target(new_test_target())?; - let branch_1 = new_test_branch(); - vb_state.set_branch(branch_1.clone())?; - let branch_2 = new_test_branch(); - vb_state.set_branch(branch_2.clone())?; - let branch_3 = new_test_branch(); - vb_state.set_branch(branch_3.clone())?; - - let iter = vb_state.list_branches()?; - assert_eq!(iter.len(), 3); - assert!(iter.contains(&branch_1)); - assert!(iter.contains(&branch_2)); - assert!(iter.contains(&branch_3)); - - Ok(()) -} diff --git a/crates/gitbutler-core/tests/virtual_branches/mod.rs b/crates/gitbutler-core/tests/virtual_branches/mod.rs index 703e0c164..d82caea52 100644 --- a/crates/gitbutler-core/tests/virtual_branches/mod.rs +++ b/crates/gitbutler-core/tests/virtual_branches/mod.rs @@ -1,2 +1 @@ mod branch; -mod iterator; From 9258614a645356c56201b4e4b12d04aab0c7cd1d Mon Sep 17 00:00:00 2001 From: Kiril Videlov Date: Sun, 7 Jul 2024 21:29:20 +0200 Subject: [PATCH 4/4] adds ci for branchstate crate --- .github/workflows/push.yaml | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/.github/workflows/push.yaml b/.github/workflows/push.yaml index 0061848e9..d78d7ed55 100644 --- a/.github/workflows/push.yaml +++ b/.github/workflows/push.yaml @@ -22,6 +22,7 @@ jobs: gitbutler-branch: ${{ steps.filter.outputs.gitbutler-branch }} gitbutler-sync: ${{ steps.filter.outputs.gitbutler-sync }} gitbutler-oplog: ${{ steps.filter.outputs.gitbutler-oplog }} + gitbutler-branchstate: ${{ steps.filter.outputs.gitbutler-branchstate }} steps: - uses: actions/checkout@v4 - uses: dorny/paths-filter@v3 @@ -67,6 +68,9 @@ jobs: gitbutler-oplog: - *rust - 'crates/gitbutler-oplog/**' + gitbutler-branchstate: + - *rust + - 'crates/gitbutler-branchstate/**' lint-node: needs: changes @@ -266,6 +270,30 @@ jobs: features: ${{ toJson(matrix.features) }} action: ${{ matrix.action }} + check-gitbutler-branchstate: + needs: changes + if: ${{ needs.changes.outputs.gitbutler-branchstate == 'true' }} + runs-on: ubuntu-latest + container: + image: ghcr.io/gitbutlerapp/ci-base-image:latest + strategy: + matrix: + action: + - test + - check + features: + - '' + - '*' + - [] + steps: + - uses: actions/checkout@v4 + - uses: ./.github/actions/init-env-rust + - uses: ./.github/actions/check-crate + with: + crate: gitbutler-branchstate + features: ${{ toJson(matrix.features) }} + action: ${{ matrix.action }} + check-gitbutler-cli: needs: changes if: ${{ needs.changes.outputs.gitbutler-cli == 'true' }} @@ -325,6 +353,7 @@ jobs: - check-gitbutler-branch - check-gitbutler-sync - check-gitbutler-oplog + - check-gitbutler-branchstate - check-gitbutler-git - check-gitbutler-cli - check-gitbutler-watcher