move branch state into the gitbutler-branch crate

Further to the move - the VirtualBranchesExt trait creates a dependency towards gitbutler-project. The gitubler-branch crate doesn't have such dependency so i deemed it undesirable to introduce it just for a convinience method. 
(Separately added an extention in gitbutler-virtual-actions since it already depends on project and to create a smaller diff)
This commit is contained in:
Kiril Videlov 2024-07-10 16:18:34 +02:00
parent 5f260f5bc4
commit e92fe44fa1
No known key found for this signature in database
GPG Key ID: A4C733025427C471
23 changed files with 62 additions and 87 deletions

22
Cargo.lock generated
View File

@ -1967,6 +1967,8 @@ dependencies = [
"anyhow",
"bstr",
"git2",
"gitbutler-error",
"gitbutler-fs",
"gitbutler-id",
"gitbutler-reference",
"gitbutler-serde",
@ -1976,6 +1978,7 @@ dependencies = [
"lazy_static",
"md5",
"serde",
"toml 0.8.14",
"tracing",
]
@ -1990,7 +1993,6 @@ dependencies = [
"git2",
"git2-hooks",
"gitbutler-branch",
"gitbutler-branchstate",
"gitbutler-command-context",
"gitbutler-commit",
"gitbutler-error",
@ -2023,21 +2025,6 @@ dependencies = [
"urlencoding",
]
[[package]]
name = "gitbutler-branchstate"
version = "0.0.0"
dependencies = [
"anyhow",
"gitbutler-branch",
"gitbutler-error",
"gitbutler-fs",
"gitbutler-project",
"gitbutler-reference",
"itertools 0.13.0",
"serde",
"toml 0.8.14",
]
[[package]]
name = "gitbutler-cli"
version = "0.0.0"
@ -2160,7 +2147,6 @@ dependencies = [
"anyhow",
"git2",
"gitbutler-branch",
"gitbutler-branchstate",
"gitbutler-fs",
"gitbutler-project",
"gitbutler-repo",
@ -2274,7 +2260,6 @@ dependencies = [
"anyhow",
"git2",
"gitbutler-branch",
"gitbutler-branchstate",
"gitbutler-command-context",
"gitbutler-error",
"gitbutler-id",
@ -2352,7 +2337,6 @@ dependencies = [
"git2",
"gitbutler-branch",
"gitbutler-branch-actions",
"gitbutler-branchstate",
"gitbutler-command-context",
"gitbutler-project",
"gitbutler-reference",

View File

@ -9,7 +9,6 @@ members = [
"crates/gitbutler-branch-actions",
"crates/gitbutler-sync",
"crates/gitbutler-oplog",
"crates/gitbutler-branchstate",
"crates/gitbutler-repo",
"crates/gitbutler-command-context",
"crates/gitbutler-feedback",
@ -54,7 +53,6 @@ gitbutler-cli = { path = "crates/gitbutler-cli" }
gitbutler-branch-actions = { path = "crates/gitbutler-branch-actions" }
gitbutler-sync = { path = "crates/gitbutler-sync" }
gitbutler-oplog = { path = "crates/gitbutler-oplog" }
gitbutler-branchstate = { path = "crates/gitbutler-branchstate" }
gitbutler-repo = { path = "crates/gitbutler-repo" }
gitbutler-command-context = { path = "crates/gitbutler-command-context" }
gitbutler-feedback = { path = "crates/gitbutler-feedback" }

View File

@ -11,7 +11,6 @@ anyhow = "1.0.86"
git2.workspace = true
tokio.workspace = true
gitbutler-oplog.workspace = true
gitbutler-branchstate.workspace = true
gitbutler-repo.workspace = true
gitbutler-user.workspace = true
gitbutler-branch.workspace = true

View File

@ -1,10 +1,10 @@
use anyhow::Result;
use gitbutler_branch::VirtualBranchesHandle;
use gitbutler_branch::{
branch::{BranchCreateRequest, BranchId, BranchUpdateRequest},
diff,
ownership::BranchOwnershipClaims,
};
use gitbutler_branchstate::{VirtualBranchesExt, VirtualBranchesHandle};
use gitbutler_command_context::ProjectRepository;
use gitbutler_oplog::{
entry::{OperationKind, SnapshotDetails},
@ -24,6 +24,7 @@ use crate::{
},
branch_manager::BranchManagerAccess,
remote::{get_branch_data, list_remote_branches, RemoteBranch, RemoteBranchData},
VirtualBranchesExt,
};
use super::r#virtual as branch;

View File

@ -6,7 +6,7 @@ use gitbutler_branch::branch::{self, BranchId};
use gitbutler_branch::diff;
use gitbutler_branch::ownership::BranchOwnershipClaims;
use gitbutler_branch::target::Target;
use gitbutler_branchstate::{VirtualBranchesExt, VirtualBranchesHandle};
use gitbutler_branch::VirtualBranchesHandle;
use gitbutler_command_context::ProjectRepository;
use gitbutler_project::FetchResult;
use gitbutler_reference::{Refname, RemoteRefname};
@ -19,7 +19,7 @@ use crate::branch_manager::BranchManagerAccess;
use crate::conflicts::RepoConflicts;
use crate::integration::{get_workspace_head, update_gitbutler_integration};
use crate::remote::{commit_to_remote_commit, RemoteCommit};
use crate::VirtualBranchHunk;
use crate::{VirtualBranchHunk, VirtualBranchesExt};
use gitbutler_branch::GITBUTLER_INTEGRATION_REFERENCE;
use gitbutler_error::error::Marker;
use gitbutler_repo::rebase::cherry_rebase;

View File

@ -3,6 +3,7 @@ use crate::{
ensure_selected_for_changes, get_applied_status,
integration::{get_integration_commiter, update_gitbutler_integration},
set_ownership, undo_commit, write_tree, NameConflitResolution, VirtualBranchHunk,
VirtualBranchesExt,
};
use anyhow::{anyhow, bail, Context, Result};
use git2::build::TreeUpdateBuilder;
@ -13,7 +14,6 @@ use gitbutler_branch::{
diff,
ownership::BranchOwnershipClaims,
};
use gitbutler_branchstate::VirtualBranchesExt;
use gitbutler_command_context::ProjectRepository;
use gitbutler_commit::commit_headers::{CommitHeadersV2, HasCommitHeaders};
use gitbutler_error::error::Marker;

View File

@ -4,18 +4,18 @@ use anyhow::{anyhow, bail, Context, Result};
use bstr::ByteSlice;
use gitbutler_branch::branch::{self, BranchCreateRequest};
use gitbutler_branch::VirtualBranchesHandle;
use gitbutler_branch::{
GITBUTLER_INTEGRATION_COMMIT_AUTHOR_EMAIL, GITBUTLER_INTEGRATION_COMMIT_AUTHOR_NAME,
GITBUTLER_INTEGRATION_REFERENCE,
};
use gitbutler_branchstate::{VirtualBranchesExt, VirtualBranchesHandle};
use gitbutler_command_context::ProjectRepository;
use gitbutler_commit::commit_ext::CommitExt;
use gitbutler_error::error::Marker;
use gitbutler_repo::{LogUntil, RepoActions, RepositoryExt};
use crate::branch_manager::BranchManagerAccess;
use crate::conflicts;
use crate::{conflicts, VirtualBranchesExt};
const WORKSPACE_HEAD: &str = "Workspace Head";

View File

@ -18,3 +18,14 @@ pub mod remote;
pub mod conflicts;
mod author;
use gitbutler_branch::VirtualBranchesHandle;
trait VirtualBranchesExt {
fn virtual_branches(&self) -> VirtualBranchesHandle;
}
impl VirtualBranchesExt for gitbutler_project::Project {
fn virtual_branches(&self) -> VirtualBranchesHandle {
VirtualBranchesHandle::new(self.gb_dir())
}
}

View File

@ -2,7 +2,7 @@ use std::path::Path;
use anyhow::{Context, Result};
use bstr::BString;
use gitbutler_branchstate::VirtualBranchesHandle;
use gitbutler_branch::VirtualBranchesHandle;
use gitbutler_command_context::ProjectRepository;
use gitbutler_commit::commit_ext::CommitExt;
use gitbutler_reference::{Refname, RemoteRefname};

View File

@ -4,7 +4,7 @@ use gitbutler_branch::diff::{self, diff_files_into_hunks, trees, FileDiff, GitHu
use gitbutler_branch::file_ownership::OwnershipClaim;
use gitbutler_branch::hunk::{Hunk, HunkHash};
use gitbutler_branch::ownership::{reconcile_claims, BranchOwnershipClaims};
use gitbutler_branchstate::{VirtualBranchesExt, VirtualBranchesHandle};
use gitbutler_branch::VirtualBranchesHandle;
use gitbutler_command_context::ProjectRepository;
use gitbutler_commit::commit_ext::CommitExt;
use gitbutler_commit::commit_headers::HasCommitHeaders;
@ -33,6 +33,7 @@ use crate::branch_manager::BranchManagerAccess;
use crate::conflicts::{self, RepoConflicts};
use crate::integration::get_workspace_head;
use crate::remote::{branch_to_remote_branch, RemoteBranch};
use crate::VirtualBranchesExt;
use gitbutler_branch::target;
use gitbutler_error::error::Code;
use gitbutler_error::error::Marker;

View File

@ -12,6 +12,7 @@ use std::{
use anyhow::{Context, Result};
use git2::TreeEntry;
use gitbutler_branch::VirtualBranchesHandle;
use gitbutler_branch::{
branch::{BranchCreateRequest, BranchUpdateRequest},
ownership::BranchOwnershipClaims,
@ -23,7 +24,6 @@ use gitbutler_branch_actions::r#virtual::{
unapply_ownership, update_branch,
};
use gitbutler_branch_actions::{branch_manager::BranchManagerAccess, integration};
use gitbutler_branchstate::VirtualBranchesExt;
use gitbutler_commit::{commit_ext::CommitExt, commit_headers::CommitHeadersV2};
use gitbutler_reference::{Refname, RemoteRefname};
use gitbutler_repo::RepositoryExt;
@ -219,7 +219,7 @@ fn create_branch_with_ownership() -> Result<()> {
virtual_branches::get_status_by_branch(project_repository, None).expect("failed to get status");
let vb_state = project_repository.project().virtual_branches();
let vb_state = VirtualBranchesHandle::new(project_repository.project().gb_dir());
let branch0 = vb_state.get_branch_in_workspace(branch0.id).unwrap();
let branch1 = branch_manager
@ -268,7 +268,7 @@ fn create_branch_in_the_middle() -> Result<()> {
})
.expect("failed to create virtual branch");
let vb_state = project_repository.project().virtual_branches();
let vb_state = VirtualBranchesHandle::new(project_repository.project().gb_dir());
let mut branches = vb_state
.list_branches_in_workspace()
.expect("failed to read branches");
@ -295,7 +295,7 @@ fn create_branch_no_arguments() -> Result<()> {
.create_virtual_branch(&BranchCreateRequest::default())
.expect("failed to create virtual branch");
let vb_state = project_repository.project().virtual_branches();
let vb_state = VirtualBranchesHandle::new(project_repository.project().gb_dir());
let branches = vb_state
.list_branches_in_workspace()
.expect("failed to read branches");
@ -473,7 +473,7 @@ fn move_hunks_multiple_sources() -> Result<()> {
"line0\nline1\nline2\nline3\nline4\nline5\nline6\nline7\nline8\nline9\nline10\nline11\nline12\nline13\n",
)?;
let vb_state = project.virtual_branches();
let vb_state = VirtualBranchesHandle::new(project_repository.project().gb_dir());
let mut branch2 = vb_state.get_branch_in_workspace(branch2_id)?;
branch2.ownership = BranchOwnershipClaims {
claims: vec!["test.txt:1-5".parse()?],
@ -798,7 +798,7 @@ fn merge_vbranch_upstream_clean_rebase() -> Result<()> {
)?;
set_test_target(project_repository)?;
let vb_state = project.virtual_branches();
let vb_state = VirtualBranchesHandle::new(project_repository.project().gb_dir());
vb_state.set_default_target(Target {
branch: "refs/remotes/origin/master".parse().unwrap(),
remote_url: "origin".to_string(),
@ -898,7 +898,7 @@ async fn merge_vbranch_upstream_conflict() -> Result<()> {
)?;
set_test_target(project_repository)?;
let vb_state = project.virtual_branches();
let vb_state = VirtualBranchesHandle::new(project.gb_dir());
vb_state.set_default_target(Target {
branch: "refs/remotes/origin/master".parse().unwrap(),
remote_url: "origin".to_string(),
@ -1325,7 +1325,7 @@ fn detect_mergeable_branch() -> Result<()> {
"line1\nline2\nline3\nline4\nbranch4\n",
)?;
let vb_state = project.virtual_branches();
let vb_state = VirtualBranchesHandle::new(project.gb_dir());
let mut branch4 = vb_state.get_branch_in_workspace(branch4_id)?;
branch4.ownership = BranchOwnershipClaims {
@ -1376,7 +1376,7 @@ fn upstream_integrated_vbranch() -> Result<()> {
(PathBuf::from("test3.txt"), "file3\n"),
]));
let vb_state = project_repository.project().virtual_branches();
let vb_state = VirtualBranchesHandle::new(project.gb_dir());
let base_commit = project_repository.repo().head().unwrap().target().unwrap();

View File

@ -1,5 +1,5 @@
use super::*;
use gitbutler_branchstate::VirtualBranchesExt;
use gitbutler_branch::VirtualBranchesHandle;
use gitbutler_oplog::oplog::Oplog;
use itertools::Itertools;
use std::io::Write;
@ -262,8 +262,7 @@ async fn restores_gitbutler_integration() -> anyhow::Result<()> {
.await?;
assert_eq!(
project
.virtual_branches()
VirtualBranchesHandle::new(project.gb_dir())
.list_branches_in_workspace()?
.len(),
0
@ -272,8 +271,7 @@ async fn restores_gitbutler_integration() -> anyhow::Result<()> {
.create_virtual_branch(project, &branch::BranchCreateRequest::default())
.await?;
assert_eq!(
project
.virtual_branches()
VirtualBranchesHandle::new(project.gb_dir())
.list_branches_in_workspace()?
.len(),
1
@ -327,7 +325,7 @@ async fn restores_gitbutler_integration() -> anyhow::Result<()> {
"head now points to the first commit, it's not commit 2 anymore"
);
let vbranches = project.virtual_branches().list_branches_in_workspace()?;
let vbranches = VirtualBranchesHandle::new(project.gb_dir()).list_branches_in_workspace()?;
assert_eq!(
vbranches.len(),
1,

View File

@ -12,7 +12,10 @@ gitbutler-reference.workspace = true
gitbutler-tagged-string.workspace = true
gitbutler-serde.workspace = true
gitbutler-id.workspace = true
gitbutler-error.workspace = true
gitbutler-fs.workspace = true
itertools = "0.13"
toml = "0.8.13"
serde = { workspace = true, features = ["std"] }
bstr = "1.9.1"
md5 = "0.7.0"

View File

@ -8,6 +8,10 @@ pub mod ownership;
pub mod serde;
pub mod target;
mod state;
pub use state::VirtualBranches as VirtualBranchesState;
pub use state::VirtualBranchesHandle;
use lazy_static::lazy_static;
lazy_static! {
pub static ref GITBUTLER_INTEGRATION_REFERENCE: gitbutler_reference::LocalRefname =

View File

@ -5,13 +5,13 @@ use std::{
use anyhow::{anyhow, Result};
use gitbutler_branch::{
use crate::{
branch::{Branch, BranchId},
target::Target,
};
use gitbutler_error::error::Code;
use gitbutler_fs::fs::read_toml_file_or_default;
use gitbutler_project::Project;
// use gitbutler_project::Project;
use gitbutler_reference::Refname;
use itertools::Itertools;
use serde::{Deserialize, Serialize};
@ -57,15 +57,15 @@ pub struct VirtualBranchesHandle {
file_path: PathBuf,
}
pub trait VirtualBranchesExt {
fn virtual_branches(&self) -> VirtualBranchesHandle;
}
// pub trait VirtualBranchesExt {
// fn virtual_branches(&self) -> VirtualBranchesHandle;
// }
impl VirtualBranchesExt for Project {
fn virtual_branches(&self) -> VirtualBranchesHandle {
VirtualBranchesHandle::new(self.gb_dir())
}
}
// impl VirtualBranchesExt 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.

View File

@ -1,17 +0,0 @@
[package]
name = "gitbutler-branchstate"
version = "0.0.0"
edition = "2021"
authors = ["GitButler <gitbutler@gitbutler.com>"]
publish = false
[dependencies]
itertools = "0.13"
anyhow = "1.0.86"
toml = "0.8.13"
serde = { workspace = true, features = ["std"]}
gitbutler-project.workspace = true
gitbutler-branch.workspace = true
gitbutler-reference.workspace = true
gitbutler-error.workspace = true
gitbutler-fs.workspace = true

View File

@ -1,4 +0,0 @@
mod state;
pub use state::VirtualBranches as VirtualBranchesState;
pub use state::VirtualBranchesExt;
pub use state::VirtualBranchesHandle;

View File

@ -8,7 +8,6 @@ publish = false
[dependencies]
anyhow = "1.0.86"
git2.workspace = true
gitbutler-branchstate.workspace = true
gitbutler-repo.workspace = true
serde = { workspace = true, features = ["std"] }
itertools = "0.13"

View File

@ -2,7 +2,7 @@ use anyhow::{anyhow, bail, Context};
use git2::{DiffOptions, FileMode};
use gitbutler_branch::branch::Branch;
use gitbutler_branch::diff::{hunks_by_filepath, FileDiff};
use gitbutler_branchstate::{VirtualBranchesExt, VirtualBranchesState};
use gitbutler_branch::{VirtualBranchesHandle, VirtualBranchesState};
use gitbutler_project::Project;
use gitbutler_repo::RepositoryExt;
use std::collections::HashMap;
@ -134,7 +134,7 @@ impl Oplog for Project {
let worktree_dir = self.path.as_path();
let repo = git2::Repository::open(worktree_dir)?;
let vb_state = self.virtual_branches();
let vb_state = VirtualBranchesHandle::new(self.gb_dir());
// grab the target commit
let default_target_commit = repo.find_commit(vb_state.get_default_target()?.sha)?;
@ -279,7 +279,7 @@ impl Oplog for Project {
oplog_state.set_oplog_head(snapshot_commit_id)?;
let vb_state = self.virtual_branches();
let vb_state = VirtualBranchesHandle::new(self.gb_dir());
let target_commit_id = vb_state.get_default_target()?.sha;
set_reference_to_oplog(&self.path, target_commit_id, snapshot_commit_id)?;
@ -711,7 +711,7 @@ fn lines_since_snapshot(project: &Project, repo: &git2::Repository) -> Result<us
return Ok(0);
};
let vbranches = project.virtual_branches().list_branches_in_workspace()?;
let vbranches = VirtualBranchesHandle::new(project.gb_dir()).list_branches_in_workspace()?;
let mut lines_changed = 0;
let dirty_branches = vbranches.iter().filter(|b| !b.ownership.claims.is_empty());
for branch in dirty_branches {

View File

@ -11,7 +11,6 @@ tracing = "0.1.40"
itertools = "0.13"
git2.workspace = true
gitbutler-oplog.workspace = true
gitbutler-branchstate.workspace = true
gitbutler-command-context.workspace = true
gitbutler-project.workspace = true
gitbutler-user.workspace = true

View File

@ -4,7 +4,7 @@ use std::time;
use anyhow::{anyhow, Context, Result};
use gitbutler_branch::target::Target;
use gitbutler_branchstate::VirtualBranchesExt;
use gitbutler_branch::VirtualBranchesHandle;
use gitbutler_command_context::ProjectRepository;
use gitbutler_error::error::Code;
use gitbutler_id::id::Id;
@ -22,7 +22,7 @@ pub async fn sync_with_gitbutler(
projects: &projects::Controller,
) -> Result<()> {
let project = project_repository.project();
let vb_state = project.virtual_branches();
let vb_state = VirtualBranchesHandle::new(project.gb_dir());
let default_target = vb_state.get_default_target()?;
let gb_code_last_commit = project
.gitbutler_code_push_state

View File

@ -18,7 +18,6 @@ keyring.workspace = true
serde_json = "1.0"
gitbutler-branch-actions = { path = "../gitbutler-branch-actions" }
gitbutler-repo = { path = "../gitbutler-repo" }
gitbutler-branchstate = { path = "../gitbutler-branchstate" }
gitbutler-command-context.workspace = true
gitbutler-project.workspace = true
gitbutler-user.workspace = true

View File

@ -19,13 +19,13 @@ pub mod paths {
pub mod virtual_branches {
use gitbutler_branch::target::Target;
use gitbutler_branchstate::VirtualBranchesExt;
use gitbutler_branch::VirtualBranchesHandle;
use gitbutler_command_context::ProjectRepository;
use crate::empty_bare_repository;
pub fn set_test_target(project_repository: &ProjectRepository) -> anyhow::Result<()> {
let vb_state = project_repository.project().virtual_branches();
let vb_state = VirtualBranchesHandle::new(project_repository.project().gb_dir());
let (remote_repo, _tmp) = empty_bare_repository();
let mut remote = project_repository
.repo()