Merge pull request #3384 from gitbutlerapp/virtual-branches-writer-refactor-1

refactor: move VirtualBranchesHandle construction out of branch reader/writer
This commit is contained in:
Kiril Videlov 2024-03-31 01:01:32 +01:00 committed by GitHub
commit e9b4ef3249
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
16 changed files with 242 additions and 123 deletions

View File

@ -35,6 +35,7 @@ jobs:
rust: &any-rust rust: &any-rust
- *rust - *rust
- 'gitbutler-!(ui)/**' - 'gitbutler-!(ui)/**'
- 'crates/**'
gitbutler-app: gitbutler-app:
- *any-rust - *any-rust
gitbutler-core: gitbutler-core:
@ -197,6 +198,7 @@ jobs:
if: always() if: always()
needs: needs:
- changes - changes
- check-gitbutler
- check-gitbutler-app - check-gitbutler-app
- check-gitbutler-changeset - check-gitbutler-changeset
- check-gitbutler-git - check-gitbutler-git

View File

@ -17,10 +17,10 @@ use crate::windows::MetadataShim;
use crate::{ use crate::{
deltas, fs, git, project_repository, deltas, fs, git, project_repository,
projects::{self, ProjectId}, projects::{self, ProjectId},
reader, sessions, reader,
sessions::SessionId, sessions::{self, SessionId},
users, users,
virtual_branches::{self, target}, virtual_branches::{self, target, VirtualBranchesHandle},
}; };
pub struct Repository { pub struct Repository {
@ -264,7 +264,10 @@ impl Repository {
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let src_target_reader = virtual_branches::target::Reader::new(&last_session_reader); let src_target_reader = virtual_branches::target::Reader::new(&last_session_reader);
let dst_target_writer = virtual_branches::target::Writer::new(self, self.project.gb_dir()) let dst_target_writer = virtual_branches::target::Writer::new(
self,
VirtualBranchesHandle::new(&self.project.gb_dir()),
)
.context("failed to open target writer for current session")?; .context("failed to open target writer for current session")?;
// copy default target // copy default target
@ -294,7 +297,10 @@ impl Repository {
.with_context(|| format!("{}: failed to write target", branch.id))?; .with_context(|| format!("{}: failed to write target", branch.id))?;
} }
let dst_branch_writer = virtual_branches::branch::Writer::new(self, self.project.gb_dir()) let dst_branch_writer = virtual_branches::branch::Writer::new(
self,
VirtualBranchesHandle::new(&self.project.gb_dir()),
)
.context("failed to open branch writer for current session")?; .context("failed to open branch writer for current session")?;
// copy branches that we don't already have // copy branches that we don't already have

View File

@ -27,3 +27,4 @@ mod remote;
pub use remote::*; pub use remote::*;
mod state; mod state;
pub use state::VirtualBranchesHandle;

View File

@ -6,7 +6,7 @@ use serde::Serialize;
use super::{ use super::{
branch, errors, branch, errors,
integration::{update_gitbutler_integration, GITBUTLER_INTEGRATION_REFERENCE}, integration::{update_gitbutler_integration, GITBUTLER_INTEGRATION_REFERENCE},
target, BranchId, RemoteCommit, target, BranchId, RemoteCommit, VirtualBranchesHandle,
}; };
use crate::{ use crate::{
gb_repository, gb_repository,
@ -188,7 +188,10 @@ pub fn set_base_branch(
sha: target_commit_oid, sha: target_commit_oid,
}; };
let target_writer = target::Writer::new(gb_repository, project_repository.project().gb_dir()) let target_writer = target::Writer::new(
gb_repository,
VirtualBranchesHandle::new(&project_repository.project().gb_dir()),
)
.context("failed to create target writer")?; .context("failed to create target writer")?;
target_writer.write_default(&target)?; target_writer.write_default(&target)?;
@ -276,8 +279,10 @@ pub fn set_base_branch(
selected_for_changes: None, selected_for_changes: None,
}; };
let branch_writer = let branch_writer = branch::Writer::new(
branch::Writer::new(gb_repository, project_repository.project().gb_dir()) gb_repository,
VirtualBranchesHandle::new(&project_repository.project().gb_dir()),
)
.context("failed to create branch writer")?; .context("failed to create branch writer")?;
branch_writer.write(&mut branch)?; branch_writer.write(&mut branch)?;
} }
@ -379,7 +384,10 @@ pub fn update_base_branch(
target.sha target.sha
))?; ))?;
let branch_writer = branch::Writer::new(gb_repository, project_repository.project().gb_dir()) let branch_writer = branch::Writer::new(
gb_repository,
VirtualBranchesHandle::new(&project_repository.project().gb_dir()),
)
.context("failed to create branch writer")?; .context("failed to create branch writer")?;
let use_context = project_repository let use_context = project_repository
@ -598,7 +606,10 @@ pub fn update_base_branch(
)?; )?;
// write new target oid // write new target oid
let target_writer = target::Writer::new(gb_repository, project_repository.project().gb_dir()) let target_writer = target::Writer::new(
gb_repository,
VirtualBranchesHandle::new(&project_repository.project().gb_dir()),
)
.context("failed to create target writer")?; .context("failed to create target writer")?;
target_writer.write_default(&target::Target { target_writer.write_default(&target::Target {
sha: new_target_commit.id(), sha: new_target_commit.id(),

View File

@ -1,5 +1,3 @@
use std::path;
use anyhow::Result; use anyhow::Result;
use super::Branch; use super::Branch;
@ -13,13 +11,12 @@ pub struct BranchWriter<'writer> {
} }
impl<'writer> BranchWriter<'writer> { impl<'writer> BranchWriter<'writer> {
pub fn new<P: AsRef<path::Path>>( pub fn new(
repository: &'writer gb_repository::Repository, repository: &'writer gb_repository::Repository,
path: P, state_handle: VirtualBranchesHandle,
) -> Result<Self, std::io::Error> { ) -> Result<Self, std::io::Error> {
let reader = reader::Reader::open(repository.root())?; let reader = reader::Reader::open(repository.root())?;
let writer = writer::DirWriter::open(repository.root())?; let writer = writer::DirWriter::open(repository.root())?;
let state_handle = VirtualBranchesHandle::new(path.as_ref());
Ok(Self { Ok(Self {
repository, repository,
writer, writer,

View File

@ -3,7 +3,7 @@ use std::io::{Read, Write};
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use lazy_static::lazy_static; use lazy_static::lazy_static;
use super::errors; use super::{errors, VirtualBranchesHandle};
use crate::{ use crate::{
gb_repository, gb_repository,
git::{self}, git::{self},
@ -262,7 +262,10 @@ fn verify_head_is_clean(
.context("failed to create virtual branch")?; .context("failed to create virtual branch")?;
// rebasing the extra commits onto the new branch // rebasing the extra commits onto the new branch
let writer = super::branch::Writer::new(gb_repository, project_repository.project().gb_dir()) let writer = super::branch::Writer::new(
gb_repository,
VirtualBranchesHandle::new(&project_repository.project().gb_dir()),
)
.context("failed to create writer")?; .context("failed to create writer")?;
extra_commits.reverse(); extra_commits.reverse();
let mut head = new_branch.head; let mut head = new_branch.head;

View File

@ -1,5 +1,3 @@
use std::path;
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use super::Target; use super::Target;
@ -17,13 +15,12 @@ pub struct TargetWriter<'writer> {
} }
impl<'writer> TargetWriter<'writer> { impl<'writer> TargetWriter<'writer> {
pub fn new<P: AsRef<path::Path>>( pub fn new(
repository: &'writer gb_repository::Repository, repository: &'writer gb_repository::Repository,
path: P, state_handle: VirtualBranchesHandle,
) -> Result<Self, std::io::Error> { ) -> Result<Self, std::io::Error> {
let reader = reader::Reader::open(&repository.root())?; let reader = reader::Reader::open(&repository.root())?;
let writer = writer::DirWriter::open(repository.root())?; let writer = writer::DirWriter::open(repository.root())?;
let state_handle = VirtualBranchesHandle::new(path.as_ref());
Ok(Self { Ok(Self {
repository, repository,
writer, writer,

View File

@ -19,6 +19,7 @@ use super::{
self, Branch, BranchCreateRequest, BranchId, BranchOwnershipClaims, Hunk, OwnershipClaim, self, Branch, BranchCreateRequest, BranchId, BranchOwnershipClaims, Hunk, OwnershipClaim,
}, },
branch_to_remote_branch, context, errors, target, Iterator, RemoteBranch, branch_to_remote_branch, context, errors, target, Iterator, RemoteBranch,
VirtualBranchesHandle,
}; };
use crate::{ use crate::{
askpass::AskpassBroker, askpass::AskpassBroker,
@ -218,7 +219,10 @@ pub fn apply_branch(
}) })
})?; })?;
let writer = branch::Writer::new(gb_repository, project_repository.project().gb_dir()) let writer = branch::Writer::new(
gb_repository,
VirtualBranchesHandle::new(&project_repository.project().gb_dir()),
)
.context("failed to create branch writer")?; .context("failed to create branch writer")?;
let mut branch = match branch::Reader::new(&current_session_reader).read(branch_id) { let mut branch = match branch::Reader::new(&current_session_reader).read(branch_id) {
@ -661,7 +665,10 @@ pub fn unapply_branch(
.find_commit(default_target.sha) .find_commit(default_target.sha)
.context("failed to find target commit")?; .context("failed to find target commit")?;
let branch_writer = branch::Writer::new(gb_repository, project_repository.project().gb_dir()) let branch_writer = branch::Writer::new(
gb_repository,
VirtualBranchesHandle::new(&project_repository.project().gb_dir()),
)
.context("failed to create writer")?; .context("failed to create writer")?;
let final_tree = if conflicts::is_resolving(project_repository) { let final_tree = if conflicts::is_resolving(project_repository) {
@ -1334,7 +1341,10 @@ pub fn create_virtual_branch(
.unwrap_or(all_virtual_branches.len()) .unwrap_or(all_virtual_branches.len())
.clamp(0, all_virtual_branches.len()); .clamp(0, all_virtual_branches.len());
let branch_writer = branch::Writer::new(gb_repository, project_repository.project().gb_dir()) let branch_writer = branch::Writer::new(
gb_repository,
VirtualBranchesHandle::new(&project_repository.project().gb_dir()),
)
.context("failed to create writer")?; .context("failed to create writer")?;
let selected_for_changes = if let Some(selected_for_changes) = create.selected_for_changes { let selected_for_changes = if let Some(selected_for_changes) = create.selected_for_changes {
@ -1553,8 +1563,10 @@ pub fn merge_virtual_branch_upstream(
let merge_tree = repo let merge_tree = repo
.find_tree(merge_tree_oid) .find_tree(merge_tree_oid)
.context("failed to find merge tree")?; .context("failed to find merge tree")?;
let branch_writer = let branch_writer = branch::Writer::new(
branch::Writer::new(gb_repository, project_repository.project().gb_dir()) gb_repository,
VirtualBranchesHandle::new(&project_repository.project().gb_dir()),
)
.context("failed to create writer")?; .context("failed to create writer")?;
if *project_repository.project().ok_with_force_push { if *project_repository.project().ok_with_force_push {
@ -1663,7 +1675,10 @@ pub fn update_branch(
let current_session_reader = sessions::Reader::open(gb_repository, &current_session) let current_session_reader = sessions::Reader::open(gb_repository, &current_session)
.context("failed to open current session")?; .context("failed to open current session")?;
let branch_reader = branch::Reader::new(&current_session_reader); let branch_reader = branch::Reader::new(&current_session_reader);
let branch_writer = branch::Writer::new(gb_repository, project_repository.project().gb_dir()) let branch_writer = branch::Writer::new(
gb_repository,
VirtualBranchesHandle::new(&project_repository.project().gb_dir()),
)
.context("failed to create writer")?; .context("failed to create writer")?;
let mut branch = branch_reader let mut branch = branch_reader
@ -1769,7 +1784,10 @@ pub fn delete_branch(
let current_session_reader = sessions::Reader::open(gb_repository, &current_session) let current_session_reader = sessions::Reader::open(gb_repository, &current_session)
.context("failed to open current session")?; .context("failed to open current session")?;
let branch_reader = branch::Reader::new(&current_session_reader); let branch_reader = branch::Reader::new(&current_session_reader);
let branch_writer = branch::Writer::new(gb_repository, project_repository.project().gb_dir()) let branch_writer = branch::Writer::new(
gb_repository,
VirtualBranchesHandle::new(&project_repository.project().gb_dir()),
)
.context("failed to create writer")?; .context("failed to create writer")?;
let branch = match branch_reader.read(branch_id) { let branch = match branch_reader.read(branch_id) {
@ -2196,8 +2214,10 @@ fn get_applied_status(
// write updated state if not resolving // write updated state if not resolving
if !project_repository.is_resolving() { if !project_repository.is_resolving() {
let branch_writer = let branch_writer = branch::Writer::new(
branch::Writer::new(gb_repository, project_repository.project().gb_dir()) gb_repository,
VirtualBranchesHandle::new(&project_repository.project().gb_dir()),
)
.context("failed to create writer")?; .context("failed to create writer")?;
for (vbranch, files) in &mut hunks_by_branch { for (vbranch, files) in &mut hunks_by_branch {
vbranch.tree = write_tree(project_repository, default_target, files)?; vbranch.tree = write_tree(project_repository, default_target, files)?;
@ -2284,7 +2304,10 @@ pub fn reset_branch(
)); ));
} }
let branch_writer = branch::Writer::new(gb_repository, project_repository.project().gb_dir()) let branch_writer = branch::Writer::new(
gb_repository,
VirtualBranchesHandle::new(&project_repository.project().gb_dir()),
)
.context("failed to create writer")?; .context("failed to create writer")?;
branch.head = target_commit_oid; branch.head = target_commit_oid;
branch_writer branch_writer
@ -2622,7 +2645,10 @@ pub fn commit(
} }
// update the virtual branch head // update the virtual branch head
let writer = branch::Writer::new(gb_repository, project_repository.project().gb_dir()) let writer = branch::Writer::new(
gb_repository,
VirtualBranchesHandle::new(&project_repository.project().gb_dir()),
)
.context("failed to create writer")?; .context("failed to create writer")?;
branch.tree = tree_oid; branch.tree = tree_oid;
branch.head = commit_oid; branch.head = commit_oid;
@ -2651,7 +2677,10 @@ pub fn push(
.map_err(errors::PushError::Other)?; .map_err(errors::PushError::Other)?;
let branch_reader = branch::Reader::new(&current_session_reader); let branch_reader = branch::Reader::new(&current_session_reader);
let branch_writer = branch::Writer::new(gb_repository, project_repository.project().gb_dir()) let branch_writer = branch::Writer::new(
gb_repository,
VirtualBranchesHandle::new(&project_repository.project().gb_dir()),
)
.context("failed to create writer")?; .context("failed to create writer")?;
let mut vbranch = branch_reader.read(branch_id).map_err(|error| match error { let mut vbranch = branch_reader.read(branch_id).map_err(|error| match error {
@ -3095,7 +3124,10 @@ pub fn amend(
) )
.context("failed to create commit")?; .context("failed to create commit")?;
let branch_writer = branch::Writer::new(gb_repository, project_repository.project().gb_dir()) let branch_writer = branch::Writer::new(
gb_repository,
VirtualBranchesHandle::new(&project_repository.project().gb_dir()),
)
.context("failed to create writer")?; .context("failed to create writer")?;
target_branch.head = commit_oid; target_branch.head = commit_oid;
branch_writer.write(target_branch)?; branch_writer.write(target_branch)?;
@ -3278,7 +3310,10 @@ pub fn cherry_pick(
.context("failed to checkout final tree")?; .context("failed to checkout final tree")?;
// update branch status // update branch status
let writer = branch::Writer::new(gb_repository, project_repository.project().gb_dir()) let writer = branch::Writer::new(
gb_repository,
VirtualBranchesHandle::new(&project_repository.project().gb_dir()),
)
.context("failed to create writer")?; .context("failed to create writer")?;
branch.head = commit_oid; branch.head = commit_oid;
writer writer
@ -3471,7 +3506,10 @@ pub fn squash(
}; };
// save new branch head // save new branch head
let writer = branch::Writer::new(gb_repository, project_repository.project().gb_dir()) let writer = branch::Writer::new(
gb_repository,
VirtualBranchesHandle::new(&project_repository.project().gb_dir()),
)
.context("failed to create writer")?; .context("failed to create writer")?;
branch.head = new_head_id; branch.head = new_head_id;
writer writer
@ -3648,7 +3686,10 @@ pub fn update_commit_message(
}; };
// save new branch head // save new branch head
let writer = branch::Writer::new(gb_repository, project_repository.project().gb_dir()) let writer = branch::Writer::new(
gb_repository,
VirtualBranchesHandle::new(&project_repository.project().gb_dir()),
)
.context("failed to create writer")?; .context("failed to create writer")?;
branch.head = new_head_id; branch.head = new_head_id;
writer writer
@ -3774,7 +3815,10 @@ pub fn move_commit(
return Err(errors::MoveCommitError::SourceLocked); return Err(errors::MoveCommitError::SourceLocked);
} }
let branch_writer = branch::Writer::new(gb_repository, project_repository.project().gb_dir()) let branch_writer = branch::Writer::new(
gb_repository,
VirtualBranchesHandle::new(&project_repository.project().gb_dir()),
)
.context("failed to create writer")?; .context("failed to create writer")?;
let branch_reader = branch::Reader::new(&latest_session_reader); let branch_reader = branch::Reader::new(&latest_session_reader);
@ -3992,7 +4036,10 @@ pub fn create_virtual_branch_from_branch(
selected_for_changes, selected_for_changes,
}; };
let writer = branch::Writer::new(gb_repository, project_repository.project().gb_dir()) let writer = branch::Writer::new(
gb_repository,
VirtualBranchesHandle::new(&project_repository.project().gb_dir()),
)
.context("failed to create writer")?; .context("failed to create writer")?;
writer writer
.write(&mut branch) .write(&mut branch)

View File

@ -17,7 +17,10 @@ pub mod paths {
} }
pub mod virtual_branches { pub mod virtual_branches {
use gitbutler_core::{gb_repository, project_repository, virtual_branches}; use gitbutler_core::{
gb_repository, project_repository,
virtual_branches::{self, VirtualBranchesHandle},
};
use crate::shared::empty_bare_repository; use crate::shared::empty_bare_repository;
@ -35,7 +38,10 @@ pub mod virtual_branches {
.expect("failed to add remote"); .expect("failed to add remote");
remote.push(&["refs/heads/master:refs/heads/master"], None)?; remote.push(&["refs/heads/master:refs/heads/master"], None)?;
virtual_branches::target::Writer::new(gb_repo, project_repository.project().gb_dir())? virtual_branches::target::Writer::new(
gb_repo,
VirtualBranchesHandle::new(&project_repository.project().gb_dir()),
)?
.write_default(&virtual_branches::target::Target { .write_default(&virtual_branches::target::Target {
branch: "refs/remotes/origin/master".parse().unwrap(), branch: "refs/remotes/origin/master".parse().unwrap(),
remote_url: remote_repo.path().to_str().unwrap().parse().unwrap(), remote_url: remote_repo.path().to_str().unwrap().parse().unwrap(),

View File

@ -1,7 +1,10 @@
use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::atomic::{AtomicUsize, Ordering};
use anyhow::Result; use anyhow::Result;
use gitbutler_core::virtual_branches::{branch, branch::BranchOwnershipClaims, Branch, BranchId}; use gitbutler_core::virtual_branches::{
branch::{self, BranchOwnershipClaims},
Branch, BranchId, VirtualBranchesHandle,
};
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use crate::shared::{Case, Suite}; use crate::shared::{Case, Suite};
@ -83,7 +86,7 @@ fn read_override() -> Result<()> {
let mut branch = test_branch(); let mut branch = test_branch();
let writer = branch::Writer::new(gb_repository, project.gb_dir())?; let writer = branch::Writer::new(gb_repository, VirtualBranchesHandle::new(&project.gb_dir()))?;
writer.write(&mut branch)?; writer.write(&mut branch)?;
let session = gb_repository.get_current_session()?.unwrap(); let session = gb_repository.get_current_session()?.unwrap();

View File

@ -4,7 +4,7 @@ use std::{
}; };
use anyhow::Context; use anyhow::Context;
use gitbutler_core::virtual_branches::branch; use gitbutler_core::virtual_branches::{branch, VirtualBranchesHandle};
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use self::branch::BranchId; use self::branch::BranchId;
@ -66,7 +66,7 @@ fn write_branch() -> anyhow::Result<()> {
let mut branch = new_test_branch(); let mut branch = new_test_branch();
let writer = branch::Writer::new(gb_repository, project.gb_dir())?; let writer = branch::Writer::new(gb_repository, VirtualBranchesHandle::new(&project.gb_dir()))?;
writer.write(&mut branch)?; writer.write(&mut branch)?;
let root = gb_repository let root = gb_repository
@ -132,7 +132,7 @@ fn should_create_session() -> anyhow::Result<()> {
let mut branch = new_test_branch(); let mut branch = new_test_branch();
let writer = branch::Writer::new(gb_repository, project.gb_dir())?; let writer = branch::Writer::new(gb_repository, VirtualBranchesHandle::new(&project.gb_dir()))?;
writer.write(&mut branch)?; writer.write(&mut branch)?;
assert!(gb_repository.get_current_session()?.is_some()); assert!(gb_repository.get_current_session()?.is_some());
@ -151,7 +151,7 @@ fn should_update() -> anyhow::Result<()> {
let mut branch = new_test_branch(); let mut branch = new_test_branch();
let writer = branch::Writer::new(gb_repository, project.gb_dir())?; let writer = branch::Writer::new(gb_repository, VirtualBranchesHandle::new(&project.gb_dir()))?;
writer.write(&mut branch)?; writer.write(&mut branch)?;
let mut updated_branch = Branch { let mut updated_branch = Branch {

View File

@ -1,7 +1,7 @@
use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::atomic::{AtomicUsize, Ordering};
use anyhow::Result; use anyhow::Result;
use gitbutler_core::virtual_branches; use gitbutler_core::virtual_branches::{self, VirtualBranchesHandle};
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use crate::shared::{Case, Suite}; use crate::shared::{Case, Suite};
@ -90,12 +90,16 @@ fn iterate_all() -> Result<()> {
.. ..
} = &suite.new_case(); } = &suite.new_case();
let target_writer = let target_writer = gitbutler_core::virtual_branches::target::Writer::new(
gitbutler_core::virtual_branches::target::Writer::new(gb_repository, project.gb_dir())?; gb_repository,
VirtualBranchesHandle::new(&project.gb_dir()),
)?;
target_writer.write_default(&new_test_target())?; target_writer.write_default(&new_test_target())?;
let branch_writer = let branch_writer = gitbutler_core::virtual_branches::branch::Writer::new(
gitbutler_core::virtual_branches::branch::Writer::new(gb_repository, project.gb_dir())?; gb_repository,
VirtualBranchesHandle::new(&project.gb_dir()),
)?;
let mut branch_1 = new_test_branch(); let mut branch_1 = new_test_branch();
branch_writer.write(&mut branch_1)?; branch_writer.write(&mut branch_1)?;
let mut branch_2 = new_test_branch(); let mut branch_2 = new_test_branch();

View File

@ -15,15 +15,15 @@ use std::{
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use gitbutler_core::{ use gitbutler_core::{
git, reader, sessions, virtual_branches, git, reader, sessions,
virtual_branches::{ virtual_branches::{
apply_branch, self, apply_branch,
branch::{BranchCreateRequest, BranchOwnershipClaims}, branch::{BranchCreateRequest, BranchOwnershipClaims},
commit, create_virtual_branch, commit, create_virtual_branch,
errors::CommitError, errors::CommitError,
integration::verify_branch, integration::verify_branch,
is_remote_branch_mergeable, is_virtual_branch_mergeable, list_remote_branches, is_remote_branch_mergeable, is_virtual_branch_mergeable, list_remote_branches,
merge_virtual_branch_upstream, unapply_ownership, update_branch, merge_virtual_branch_upstream, unapply_ownership, update_branch, VirtualBranchesHandle,
}, },
}; };
use pretty_assertions::assert_eq; use pretty_assertions::assert_eq;
@ -614,7 +614,10 @@ fn move_hunks_multiple_sources() -> Result<()> {
let current_session = gb_repository.get_or_create_current_session()?; let current_session = gb_repository.get_or_create_current_session()?;
let current_session_reader = sessions::Reader::open(gb_repository, &current_session)?; let current_session_reader = sessions::Reader::open(gb_repository, &current_session)?;
let branch_reader = virtual_branches::branch::Reader::new(&current_session_reader); let branch_reader = virtual_branches::branch::Reader::new(&current_session_reader);
let branch_writer = virtual_branches::branch::Writer::new(gb_repository, project.gb_dir())?; let branch_writer = virtual_branches::branch::Writer::new(
gb_repository,
VirtualBranchesHandle::new(&project.gb_dir()),
)?;
let mut branch2 = branch_reader.read(&branch2_id)?; let mut branch2 = branch_reader.read(&branch2_id)?;
branch2.ownership = BranchOwnershipClaims { branch2.ownership = BranchOwnershipClaims {
claims: vec!["test.txt:1-5".parse()?], claims: vec!["test.txt:1-5".parse()?],
@ -893,7 +896,10 @@ fn merge_vbranch_upstream_clean_rebase() -> Result<()> {
)?; )?;
set_test_target(gb_repository, project_repository)?; set_test_target(gb_repository, project_repository)?;
virtual_branches::target::Writer::new(gb_repository, project_repository.project().gb_dir())? virtual_branches::target::Writer::new(
gb_repository,
VirtualBranchesHandle::new(&project_repository.project().gb_dir()),
)?
.write_default(&virtual_branches::target::Target { .write_default(&virtual_branches::target::Target {
branch: "refs/remotes/origin/master".parse().unwrap(), branch: "refs/remotes/origin/master".parse().unwrap(),
remote_url: "origin".to_string(), remote_url: "origin".to_string(),
@ -905,7 +911,10 @@ fn merge_vbranch_upstream_clean_rebase() -> Result<()> {
std::fs::write(Path::new(&project.path).join(file_path2), "file2\n")?; std::fs::write(Path::new(&project.path).join(file_path2), "file2\n")?;
let remote_branch: git::RemoteRefname = "refs/remotes/origin/master".parse().unwrap(); let remote_branch: git::RemoteRefname = "refs/remotes/origin/master".parse().unwrap();
let branch_writer = virtual_branches::branch::Writer::new(gb_repository, project.gb_dir())?; let branch_writer = virtual_branches::branch::Writer::new(
gb_repository,
VirtualBranchesHandle::new(&project.gb_dir()),
)?;
let mut branch = create_virtual_branch( let mut branch = create_virtual_branch(
gb_repository, gb_repository,
project_repository, project_repository,
@ -1018,13 +1027,15 @@ fn merge_vbranch_upstream_conflict() -> Result<()> {
)?; )?;
set_test_target(gb_repository, project_repository)?; set_test_target(gb_repository, project_repository)?;
virtual_branches::target::Writer::new(gb_repository, project.gb_dir())?.write_default( virtual_branches::target::Writer::new(
&virtual_branches::target::Target { gb_repository,
VirtualBranchesHandle::new(&project.gb_dir()),
)?
.write_default(&virtual_branches::target::Target {
branch: "refs/remotes/origin/master".parse().unwrap(), branch: "refs/remotes/origin/master".parse().unwrap(),
remote_url: "origin".to_string(), remote_url: "origin".to_string(),
sha: target_oid, sha: target_oid,
}, })?;
)?;
// add some uncommitted work // add some uncommitted work
std::fs::write( std::fs::write(
@ -1033,7 +1044,10 @@ fn merge_vbranch_upstream_conflict() -> Result<()> {
)?; )?;
let remote_branch: git::RemoteRefname = "refs/remotes/origin/master".parse().unwrap(); let remote_branch: git::RemoteRefname = "refs/remotes/origin/master".parse().unwrap();
let branch_writer = virtual_branches::branch::Writer::new(gb_repository, project.gb_dir())?; let branch_writer = virtual_branches::branch::Writer::new(
gb_repository,
VirtualBranchesHandle::new(&project.gb_dir()),
)?;
let mut branch = create_virtual_branch( let mut branch = create_virtual_branch(
gb_repository, gb_repository,
project_repository, project_repository,
@ -1389,7 +1403,10 @@ fn detect_mergeable_branch() -> Result<()> {
let current_session = gb_repository.get_or_create_current_session()?; let current_session = gb_repository.get_or_create_current_session()?;
let current_session_reader = sessions::Reader::open(gb_repository, &current_session)?; let current_session_reader = sessions::Reader::open(gb_repository, &current_session)?;
let branch_reader = virtual_branches::branch::Reader::new(&current_session_reader); let branch_reader = virtual_branches::branch::Reader::new(&current_session_reader);
let branch_writer = virtual_branches::branch::Writer::new(gb_repository, project.gb_dir())?; let branch_writer = virtual_branches::branch::Writer::new(
gb_repository,
VirtualBranchesHandle::new(&project.gb_dir()),
)?;
update_branch( update_branch(
gb_repository, gb_repository,
@ -1580,7 +1597,10 @@ fn upstream_integrated_vbranch() -> Result<()> {
"update target", "update target",
)?; )?;
virtual_branches::target::Writer::new(gb_repository, project_repository.project().gb_dir())? virtual_branches::target::Writer::new(
gb_repository,
VirtualBranchesHandle::new(&project_repository.project().gb_dir()),
)?
.write_default(&virtual_branches::target::Target { .write_default(&virtual_branches::target::Target {
branch: "refs/remotes/origin/master".parse().unwrap(), branch: "refs/remotes/origin/master".parse().unwrap(),
remote_url: "http://origin.com/project".to_string(), remote_url: "http://origin.com/project".to_string(),

View File

@ -1,7 +1,10 @@
use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::atomic::{AtomicUsize, Ordering};
use anyhow::Result; use anyhow::Result;
use gitbutler_core::virtual_branches::{target, target::Target, BranchId}; use gitbutler_core::virtual_branches::{
target::{self, Target},
BranchId, VirtualBranchesHandle,
};
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use crate::shared::{Case, Suite}; use crate::shared::{Case, Suite};
@ -129,14 +132,17 @@ fn read_override_target() -> Result<()> {
sha: "0123456789abcdef0123456789abcdef01234567".parse().unwrap(), sha: "0123456789abcdef0123456789abcdef01234567".parse().unwrap(),
}; };
let branch_writer = let branch_writer = gitbutler_core::virtual_branches::branch::Writer::new(
gitbutler_core::virtual_branches::branch::Writer::new(gb_repository, project.gb_dir())?; gb_repository,
VirtualBranchesHandle::new(&project.gb_dir()),
)?;
branch_writer.write(&mut branch)?; branch_writer.write(&mut branch)?;
let session = gb_repository.get_current_session()?.unwrap(); let session = gb_repository.get_current_session()?.unwrap();
let session_reader = gitbutler_core::sessions::Reader::open(gb_repository, &session)?; let session_reader = gitbutler_core::sessions::Reader::open(gb_repository, &session)?;
let target_writer = target::Writer::new(gb_repository, project.gb_dir())?; let target_writer =
target::Writer::new(gb_repository, VirtualBranchesHandle::new(&project.gb_dir()))?;
let reader = target::Reader::new(&session_reader); let reader = target::Reader::new(&session_reader);
target_writer.write_default(&default_target)?; target_writer.write_default(&default_target)?;

View File

@ -4,7 +4,11 @@ use std::{
}; };
use anyhow::Context; use anyhow::Context;
use gitbutler_core::virtual_branches::{branch, target, target::Target, BranchId}; use gitbutler_core::virtual_branches::{
branch,
target::{self, Target},
BranchId, VirtualBranchesHandle,
};
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use crate::shared::{Case, Suite}; use crate::shared::{Case, Suite};
@ -69,10 +73,12 @@ fn write() -> anyhow::Result<()> {
sha: "0123456789abcdef0123456789abcdef01234567".parse().unwrap(), sha: "0123456789abcdef0123456789abcdef01234567".parse().unwrap(),
}; };
let branch_writer = branch::Writer::new(gb_repository, project.gb_dir())?; let branch_writer =
branch::Writer::new(gb_repository, VirtualBranchesHandle::new(&project.gb_dir()))?;
branch_writer.write(&mut branch)?; branch_writer.write(&mut branch)?;
let target_writer = target::Writer::new(gb_repository, project.gb_dir())?; let target_writer =
target::Writer::new(gb_repository, VirtualBranchesHandle::new(&project.gb_dir()))?;
target_writer.write(&branch.id, &target)?; target_writer.write(&branch.id, &target)?;
let root = gb_repository let root = gb_repository
@ -161,9 +167,11 @@ fn should_update() -> anyhow::Result<()> {
sha: "0123456789abcdef0123456789abcdef01234567".parse().unwrap(), sha: "0123456789abcdef0123456789abcdef01234567".parse().unwrap(),
}; };
let branch_writer = branch::Writer::new(gb_repository, project.gb_dir())?; let branch_writer =
branch::Writer::new(gb_repository, VirtualBranchesHandle::new(&project.gb_dir()))?;
branch_writer.write(&mut branch)?; branch_writer.write(&mut branch)?;
let target_writer = target::Writer::new(gb_repository, project.gb_dir())?; let target_writer =
target::Writer::new(gb_repository, VirtualBranchesHandle::new(&project.gb_dir()))?;
target_writer.write(&branch.id, &target)?; target_writer.write(&branch.id, &target)?;
let updated_target = Target { let updated_target = Target {

View File

@ -9,7 +9,7 @@ use gitbutler_app::watcher::handlers::calculate_deltas_handler::Handler;
use gitbutler_core::{ use gitbutler_core::{
deltas::{self, operations::Operation}, deltas::{self, operations::Operation},
reader, sessions, reader, sessions,
virtual_branches::{self, branch}, virtual_branches::{self, branch, VirtualBranchesHandle},
}; };
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
@ -637,8 +637,12 @@ fn should_persist_branches_targets_state_between_sessions() -> Result<()> {
} = &suite.new_case_with_files(HashMap::from([(PathBuf::from("test.txt"), "hello world")])); } = &suite.new_case_with_files(HashMap::from([(PathBuf::from("test.txt"), "hello world")]));
let listener = Handler::from_path(suite.local_app_data()); let listener = Handler::from_path(suite.local_app_data());
let branch_writer = branch::Writer::new(gb_repository, project.gb_dir())?; let branch_writer =
let target_writer = virtual_branches::target::Writer::new(gb_repository, project.gb_dir())?; branch::Writer::new(gb_repository, VirtualBranchesHandle::new(&project.gb_dir()))?;
let target_writer = virtual_branches::target::Writer::new(
gb_repository,
VirtualBranchesHandle::new(&project.gb_dir()),
)?;
let default_target = new_test_target(); let default_target = new_test_target();
target_writer.write_default(&default_target)?; target_writer.write_default(&default_target)?;
let mut vbranch0 = new_test_branch(); let mut vbranch0 = new_test_branch();
@ -690,8 +694,12 @@ fn should_restore_branches_targets_state_from_head_session() -> Result<()> {
} = &suite.new_case_with_files(HashMap::from([(PathBuf::from("test.txt"), "hello world")])); } = &suite.new_case_with_files(HashMap::from([(PathBuf::from("test.txt"), "hello world")]));
let listener = Handler::from_path(suite.local_app_data()); let listener = Handler::from_path(suite.local_app_data());
let branch_writer = branch::Writer::new(gb_repository, project.gb_dir())?; let branch_writer =
let target_writer = virtual_branches::target::Writer::new(gb_repository, project.gb_dir())?; branch::Writer::new(gb_repository, VirtualBranchesHandle::new(&project.gb_dir()))?;
let target_writer = virtual_branches::target::Writer::new(
gb_repository,
VirtualBranchesHandle::new(&project.gb_dir()),
)?;
let default_target = new_test_target(); let default_target = new_test_target();
target_writer.write_default(&default_target)?; target_writer.write_default(&default_target)?;
let mut vbranch0 = new_test_branch(); let mut vbranch0 = new_test_branch();