Merge pull request #3709 from gitbutlerapp/cleanups

A few cleanups of virtual.rs
This commit is contained in:
Josh Junon 2024-05-06 20:36:06 +02:00 committed by GitHub
commit 0ba06e4292
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 116 additions and 116 deletions

View File

@ -21,7 +21,7 @@ use crate::{
reader,
sessions::{self, SessionId},
users,
virtual_branches::{target, VirtualBranchesHandle},
virtual_branches::target,
};
pub struct Repository {
@ -233,7 +233,7 @@ impl Repository {
// take branches from the last session and put them into the current session
fn copy_branches(&self) -> Result<()> {
let vb_state = VirtualBranchesHandle::new(&self.project.gb_dir());
let vb_state = self.project.virtual_branches();
let branches = vb_state
.list_branches()
@ -491,7 +491,7 @@ impl Repository {
}
pub fn default_target(&self) -> Result<Option<target::Target>> {
let vb_state = VirtualBranchesHandle::new(&self.project.gb_dir());
let vb_state = self.project.virtual_branches();
match vb_state.get_default_target() {
Result::Ok(target) => Ok(Some(target)),
Err(reader::Error::NotFound) => Ok(None),

View File

@ -5,7 +5,9 @@ use std::{
use serde::{Deserialize, Serialize};
use crate::{git, id::Id, types::default_true::DefaultTrue};
use crate::{
git, id::Id, types::default_true::DefaultTrue, virtual_branches::VirtualBranchesHandle,
};
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
@ -93,12 +95,6 @@ fn default_snapshot_lines_threshold() -> usize {
20
}
impl AsRef<Project> for Project {
fn as_ref(&self) -> &Project {
self
}
}
impl Project {
pub fn is_sync_enabled(&self) -> bool {
self.api.as_ref().map(|api| api.sync).unwrap_or_default()
@ -117,4 +113,9 @@ impl Project {
pub fn gb_dir(&self) -> PathBuf {
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())
}
}

View File

@ -6,7 +6,7 @@ use std::str::FromStr;
use anyhow::Result;
use crate::{projects::Project, virtual_branches::VirtualBranchesHandle};
use crate::projects::Project;
use super::{
entry::{OperationType, Snapshot, SnapshotDetails, Trailer},
@ -65,7 +65,7 @@ impl Oplog for Project {
let repo_path = self.path.as_path();
let repo = git2::Repository::init(repo_path)?;
let vb_state = VirtualBranchesHandle::new(&self.gb_dir());
let vb_state = self.virtual_branches();
let default_target_sha = vb_state.get_default_target()?.sha;
let oplog_state = OplogHandle::new(&self.gb_dir());
@ -461,7 +461,7 @@ mod tests {
// create gb_dir folder
std::fs::create_dir_all(project.gb_dir()).unwrap();
let vb_state = VirtualBranchesHandle::new(&project.gb_dir());
let vb_state = project.virtual_branches();
let target_sha = initial_commit.to_string();
let default_target = crate::virtual_branches::target::Target {

View File

@ -66,7 +66,7 @@ fn go_back_to_integration(
return Err(errors::SetBaseBranchError::DirtyWorkingDirectory);
}
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
let vb_state = project_repository.project().virtual_branches();
let all_virtual_branches = vb_state
.list_branches()
.context("failed to read virtual branches")?;
@ -183,7 +183,7 @@ pub fn set_base_branch(
push_remote_name: None,
};
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
let vb_state = project_repository.project().virtual_branches();
vb_state.set_default_target(target.clone())?;
// TODO: make sure this is a real branch
@ -297,7 +297,7 @@ pub fn set_target_push_remote(
.context("failed to get remote name")?
.to_string(),
);
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
let vb_state = project_repository.project().virtual_branches();
vb_state.set_default_target(target.clone())?;
}
@ -386,7 +386,7 @@ pub fn update_base_branch(
target.sha
))?;
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
let vb_state = project_repository.project().virtual_branches();
let integration_commit = get_workspace_head(&vb_state, project_repository)?;
// try to update every branch

View File

@ -1,7 +1,7 @@
use std::{fmt::Display, ops::RangeInclusive, str::FromStr};
use anyhow::{anyhow, Context, Result};
use bstr::{BStr, ByteSlice};
use bstr::ByteSlice;
use crate::git::diff;
@ -21,7 +21,7 @@ impl From<&diff::GitHunk> for Hunk {
Hunk {
start: hunk.new_start,
end: hunk.new_start + hunk.new_lines,
hash: Some(Hunk::hash_diff(hunk.diff_lines.as_ref())),
hash: Some(Hunk::hash_diff(&hunk.diff_lines)),
timestamp_ms: None,
locked_to: hunk.locked_to.to_vec(),
}
@ -100,7 +100,7 @@ impl FromStr for Hunk {
impl Display for Hunk {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}-{}", self.start, self.end)?;
match (self.hash.as_ref(), self.timestamp_ms.as_ref()) {
match (&self.hash, &self.timestamp_ms) {
(Some(hash), Some(timestamp_ms)) => write!(f, "-{:x}-{}", hash, timestamp_ms),
(Some(hash), None) => write!(f, "-{:x}", hash),
(None, Some(timestamp_ms)) => write!(f, "--{}", timestamp_ms),
@ -139,7 +139,7 @@ impl Hunk {
self
}
pub fn timestam_ms(&self) -> Option<u128> {
pub fn timestamp_ms(&self) -> Option<u128> {
self.timestamp_ms
}
@ -166,7 +166,8 @@ impl Hunk {
/// Note that there is danger in changing the hash function as this information is persisted
/// in the virtual-branch toml file. Even if it can still be parsed or decoded,
/// these values have to remain consistent.
pub fn hash_diff(diff: &BStr) -> HunkHash {
pub fn hash_diff<S: AsRef<[u8]>>(diff: S) -> HunkHash {
let diff = diff.as_ref();
if !diff.starts_with(b"@@") {
return Self::hash(diff);
}
@ -178,7 +179,8 @@ impl Hunk {
}
/// Produce a hash of `input` using the same function as [`Self::hash_diff()`], but without any assumptions.
pub fn hash(input: &[u8]) -> HunkHash {
md5::compute(input)
#[inline]
pub fn hash<S: AsRef<[u8]>>(input: S) -> HunkHash {
md5::compute(input.as_ref())
}
}

View File

@ -39,7 +39,7 @@ pub fn get_workspace_head(
.get_default_target()
.context("failed to get target")?;
let repo = &project_repository.git_repository;
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
let vb_state = project_repository.project().virtual_branches();
let all_virtual_branches = vb_state.list_branches()?;
let applied_virtual_branches = all_virtual_branches
@ -170,7 +170,7 @@ pub fn update_gitbutler_integration_with_commit(
repo.set_head(&GITBUTLER_INTEGRATION_REFERENCE.clone().into())
.context("failed to set head")?;
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
let vb_state = project_repository.project().virtual_branches();
// get all virtual branches, we need to try to update them all
let all_virtual_branches = vb_state
@ -343,7 +343,7 @@ fn verify_head_is_clean(
.context("failed to create virtual branch")?;
// rebasing the extra commits onto the new branch
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
let vb_state = project_repository.project().virtual_branches();
extra_commits.reverse();
let mut head = new_branch.head;
for commit in extra_commits {

View File

@ -30,8 +30,8 @@ pub struct VirtualBranchesHandle {
impl VirtualBranchesHandle {
/// Creates a new concurrency-safe handle to the state of virtual branches.
pub fn new(base_path: &Path) -> Self {
let file_path = base_path.join("virtual_branches.toml");
pub fn new<P: AsRef<Path>>(base_path: P) -> Self {
let file_path = base_path.as_ref().join("virtual_branches.toml");
Self { file_path }
}

View File

@ -1,4 +1,4 @@
use std::borrow::{Borrow, Cow};
use std::borrow::Borrow;
#[cfg(target_family = "unix")]
use std::os::unix::prelude::PermissionsExt;
use std::time::SystemTime;
@ -10,9 +10,10 @@ use std::{
};
use anyhow::{anyhow, bail, Context, Result};
use bstr::{BStr, BString, ByteSlice, ByteVec};
use bstr::{BString, ByteSlice, ByteVec};
use diffy::{apply_bytes as diffy_apply, Line, Patch};
use git2_hooks::HookResult;
use hex::ToHex;
use regex::Regex;
use serde::Serialize;
@ -160,7 +161,7 @@ impl VirtualBranchHunk {
hunk: GitHunk,
mtimes: &mut MTimeCache,
) -> Self {
let hash = Hunk::hash_diff(hunk.diff_lines.as_ref());
let hash = Hunk::hash_diff(&hunk.diff_lines);
Self {
id: Self::gen_id(hunk.new_start, hunk.new_lines),
modified_at: mtimes.mtime_by_path(project_path.join(&file_path)),
@ -225,7 +226,7 @@ pub fn apply_branch(
}
let repo = &project_repository.git_repository;
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
let vb_state = project_repository.project().virtual_branches();
let Some(default_target) = vb_state
.try_get_default_target()
.context("failed to get default target")?
@ -480,7 +481,7 @@ pub fn unapply_ownership(
));
}
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
let vb_state = project_repository.project().virtual_branches();
let Some(default_target) = vb_state
.try_get_default_target()
@ -627,7 +628,7 @@ pub fn unapply_branch(
project_repository: &project_repository::Repository,
branch_id: &BranchId,
) -> Result<Option<branch::Branch>, errors::UnapplyBranchError> {
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
let vb_state = project_repository.project().virtual_branches();
let mut target_branch = vb_state
.get_branch(branch_id)
@ -782,7 +783,7 @@ pub fn list_virtual_branches(
) -> Result<(Vec<VirtualBranch>, Vec<diff::FileDiff>), errors::ListVirtualBranchesError> {
let mut branches: Vec<VirtualBranch> = Vec::new();
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
let vb_state = project_repository.project().virtual_branches();
let default_target = vb_state
.get_default_target()
.context("failed to get default target")?;
@ -1056,7 +1057,7 @@ pub fn create_virtual_branch(
project_repository: &project_repository::Repository,
create: &BranchCreateRequest,
) -> Result<branch::Branch, errors::CreateVirtualBranchError> {
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
let vb_state = project_repository.project().virtual_branches();
let Some(default_target) = vb_state
.try_get_default_target()
@ -1176,7 +1177,7 @@ pub fn merge_virtual_branch_upstream(
));
}
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
let vb_state = project_repository.project().virtual_branches();
let mut branch = match vb_state.get_branch(branch_id) {
Ok(branch) => Ok(branch),
@ -1382,7 +1383,7 @@ pub fn update_branch(
project_repository: &project_repository::Repository,
branch_update: branch::BranchUpdateRequest,
) -> Result<branch::Branch, errors::UpdateBranchError> {
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
let vb_state = project_repository.project().virtual_branches();
let mut branch = vb_state
.get_branch(&branch_update.id)
.map_err(|error| match error {
@ -1480,7 +1481,7 @@ pub fn delete_branch(
project_repository: &project_repository::Repository,
branch_id: &BranchId,
) -> Result<(), Error> {
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
let vb_state = project_repository.project().virtual_branches();
let branch = match vb_state.get_branch(branch_id) {
Ok(branch) => Ok(branch),
Err(reader::Error::NotFound) => return Ok(()),
@ -1566,9 +1567,10 @@ fn set_ownership(
struct MTimeCache(HashMap<PathBuf, u128>);
impl MTimeCache {
fn mtime_by_path<'a>(&mut self, path: impl Into<Cow<'a, Path>>) -> u128 {
let path = path.into();
if let Some(mtime) = self.0.get(path.as_ref()) {
fn mtime_by_path<P: AsRef<Path>>(&mut self, path: P) -> u128 {
let path = path.as_ref();
if let Some(mtime) = self.0.get(path) {
return *mtime;
}
@ -1585,7 +1587,7 @@ impl MTimeCache {
)
.duration_since(time::UNIX_EPOCH)
.map_or(0, |d| d.as_millis());
self.0.insert(path.into_owned(), mtime);
self.0.insert(path.into(), mtime);
mtime
}
}
@ -1626,7 +1628,7 @@ pub fn get_status_by_branch(
project_repository: &project_repository::Repository,
integration_commit: Option<&git::Oid>,
) -> Result<(AppliedStatuses, Vec<diff::FileDiff>)> {
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
let vb_state = project_repository.project().virtual_branches();
let Some(default_target) = vb_state
.try_get_default_target()
@ -1791,7 +1793,7 @@ fn get_applied_status(
for blame_hunk in blame.iter() {
let commit_id = git::Oid::from(blame_hunk.orig_commit_id());
if commit_id != *target_sha && commit_id != *integration_commit {
let hash = Hunk::hash_diff(hunk.diff_lines.as_ref());
let hash = Hunk::hash_diff(&hunk.diff_lines);
let id = commit_to_branch.get(&commit_id).map(|id| id.to_string());
let branch_id = if let Some(id) = id {
uuid::Uuid::parse_str(&id)?
@ -1844,12 +1846,12 @@ fn get_applied_status(
.filter_map(|claimed_hunk| {
// if any of the current hunks intersects with the owned hunk, we want to keep it
for (i, git_diff_hunk) in git_diff_hunks.iter().enumerate() {
let hash = Hunk::hash_diff(git_diff_hunk.diff_lines.as_ref());
let hash = Hunk::hash_diff(&git_diff_hunk.diff_lines);
if locked_hunk_map.contains_key(&hash) {
return None; // Defer allocation to unclaimed hunks processing
}
if claimed_hunk.eq(&Hunk::from(git_diff_hunk)) {
let timestamp = claimed_hunk.timestam_ms().unwrap_or(mtime);
let timestamp = claimed_hunk.timestamp_ms().unwrap_or(mtime);
diffs_by_branch
.entry(branch.id)
.or_default()
@ -1915,7 +1917,7 @@ fn get_applied_status(
// process the remaining ones.
for (filepath, hunks) in base_diffs {
for hunk in hunks {
let hash = Hunk::hash_diff(hunk.diff_lines.as_ref());
let hash = Hunk::hash_diff(&hunk.diff_lines);
let locked_to = locked_hunk_map.get(&hash);
let vbranch_pos = if let Some(locks) = locked_to {
@ -1931,7 +1933,7 @@ fn get_applied_status(
default_vbranch_pos
};
let hash = Hunk::hash_diff(hunk.diff_lines.as_ref());
let hash = Hunk::hash_diff(&hunk.diff_lines);
let mut new_hunk = Hunk::from(&hunk)
.with_timestamp(mtimes.mtime_by_path(filepath.as_path()))
.with_hash(hash);
@ -1944,7 +1946,7 @@ fn get_applied_status(
file_path: filepath.clone(),
hunks: vec![Hunk::from(&hunk)
.with_timestamp(mtimes.mtime_by_path(filepath.as_path()))
.with_hash(Hunk::hash_diff(hunk.diff_lines.as_ref()))],
.with_hash(Hunk::hash_diff(&hunk.diff_lines))],
});
let hunk = match locked_to {
@ -1976,7 +1978,7 @@ fn get_applied_status(
// write updated state if not resolving
if !project_repository.is_resolving() {
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
let vb_state = project_repository.project().virtual_branches();
for (vbranch, files) in &mut hunks_by_branch {
vbranch.tree = write_tree(project_repository, &vbranch.head, files)?;
vb_state
@ -2021,7 +2023,7 @@ pub fn reset_branch(
branch_id: &BranchId,
target_commit_oid: git::Oid,
) -> Result<(), errors::ResetBranchError> {
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
let vb_state = project_repository.project().virtual_branches();
let Some(default_target) = vb_state
.try_get_default_target()
@ -2080,7 +2082,7 @@ pub fn reset_branch(
// Assign the new hunks to the branch we're working on.
for (path, filediff) in diff {
for hunk in filediff.hunks {
let hash = Hunk::hash_diff(hunk.diff_lines.as_ref());
let hash = Hunk::hash_diff(&hunk.diff_lines);
branch.ownership.put(
format!(
"{}:{}-{}-{:?}",
@ -2234,7 +2236,7 @@ pub fn write_tree_onto_tree(
}
let patch = Patch::from_bytes(&all_diffs)?;
let blob_contents = apply(blob_contents.into(), &patch).context(format!(
let blob_contents = apply(blob_contents, &patch).context(format!(
"failed to apply\n{}\nonto:\n{}",
all_diffs.as_bstr(),
blob_contents.as_bstr()
@ -2252,7 +2254,7 @@ pub fn write_tree_onto_tree(
hunks.sort_by_key(|hunk| hunk.new_start);
for hunk in hunks {
let patch = Patch::from_bytes(&hunk.diff_lines)?;
blob_contents = apply(blob_contents.as_ref(), &patch)
blob_contents = apply(&blob_contents, &patch)
.context(format!("failed to apply {}", &hunk.diff_lines))?;
}
@ -2270,9 +2272,6 @@ pub fn write_tree_onto_tree(
} else if base_tree.get_path(rel_path).is_ok() {
// remove file from index if it exists in the base tree
builder.remove(rel_path);
} else {
// file not in index or base tree, do nothing
// this is the
}
}
@ -2314,7 +2313,7 @@ pub fn commit(
run_hooks: bool,
) -> Result<git::Oid, errors::CommitError> {
let mut message_buffer = message.to_owned();
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
let vb_state = project_repository.project().virtual_branches();
if run_hooks {
let hook_result = project_repository
@ -2427,7 +2426,7 @@ pub fn commit(
.context("failed to run hook")?;
}
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
let vb_state = project_repository.project().virtual_branches();
branch.tree = tree_oid;
branch.head = commit_oid;
vb_state
@ -2447,7 +2446,7 @@ pub fn push(
credentials: &git::credentials::Helper,
askpass: Option<(AskpassBroker, Option<BranchId>)>,
) -> Result<(), errors::PushError> {
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
let vb_state = project_repository.project().virtual_branches();
let mut vbranch = vb_state
.get_branch(branch_id)
@ -2459,7 +2458,7 @@ pub fn push(
error => errors::PushError::Other(error.into()),
})?;
let remote_branch = if let Some(upstream_branch) = vbranch.upstream.as_ref() {
let remote_branch = if let Some(upstream_branch) = &vbranch.upstream {
upstream_branch.clone()
} else {
let Some(default_target) = vb_state
@ -2600,7 +2599,7 @@ pub fn is_remote_branch_mergeable(
project_repository: &project_repository::Repository,
branch_name: &git::RemoteRefname,
) -> Result<bool, errors::IsRemoteBranchMergableError> {
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
let vb_state = project_repository.project().virtual_branches();
let Some(default_target) = vb_state
.try_get_default_target()
@ -2656,7 +2655,7 @@ pub fn is_virtual_branch_mergeable(
project_repository: &project_repository::Repository,
branch_id: &BranchId,
) -> Result<bool, errors::IsVirtualBranchMergeable> {
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
let vb_state = project_repository.project().virtual_branches();
let branch = match vb_state.get_branch(branch_id) {
Ok(branch) => Ok(branch),
Err(reader::Error::NotFound) => Err(errors::IsVirtualBranchMergeable::BranchNotFound(
@ -2740,7 +2739,7 @@ pub fn move_commit_file(
to_commit_oid: git::Oid,
target_ownership: &BranchOwnershipClaims,
) -> Result<git::Oid, errors::VirtualBranchError> {
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
let vb_state = project_repository.project().virtual_branches();
let mut target_branch = match vb_state.get_branch(branch_id) {
Ok(branch) => Ok(branch),
@ -3012,7 +3011,7 @@ pub fn amend(
));
}
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
let vb_state = project_repository.project().virtual_branches();
let all_branches = vb_state
.list_branches()
@ -3195,7 +3194,7 @@ pub fn reorder_commit(
commit_oid: git::Oid,
offset: i32,
) -> Result<(), errors::VirtualBranchError> {
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
let vb_state = project_repository.project().virtual_branches();
let Some(default_target) = vb_state
.try_get_default_target()
@ -3305,7 +3304,7 @@ pub fn insert_blank_commit(
user: Option<&users::User>,
offset: i32,
) -> Result<(), errors::VirtualBranchError> {
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
let vb_state = project_repository.project().virtual_branches();
let mut branch = match vb_state.get_branch(branch_id) {
Ok(branch) => Ok(branch),
@ -3372,7 +3371,7 @@ pub fn undo_commit(
branch_id: &BranchId,
commit_oid: git::Oid,
) -> Result<(), errors::VirtualBranchError> {
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
let vb_state = project_repository.project().virtual_branches();
let mut branch = match vb_state.get_branch(branch_id) {
Ok(branch) => Ok(branch),
@ -3585,7 +3584,7 @@ pub fn cherry_pick(
}));
}
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
let vb_state = project_repository.project().virtual_branches();
let mut branch = vb_state
.get_branch(branch_id)
@ -3768,7 +3767,7 @@ pub fn squash(
}));
}
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
let vb_state = project_repository.project().virtual_branches();
let Some(default_target) = vb_state
.try_get_default_target()
@ -3904,7 +3903,7 @@ pub fn update_commit_message(
));
}
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
let vb_state = project_repository.project().virtual_branches();
let Some(default_target) = vb_state
.try_get_default_target()
@ -4022,7 +4021,7 @@ pub fn move_commit(
));
}
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
let vb_state = project_repository.project().virtual_branches();
let applied_branches = vb_state
.list_branches()
@ -4194,7 +4193,7 @@ pub fn create_virtual_branch_from_branch(
));
}
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
let vb_state = project_repository.project().virtual_branches();
let Some(default_target) = vb_state
.try_get_default_target()
@ -4337,9 +4336,9 @@ pub fn create_virtual_branch_from_branch(
}
/// Just like [`diffy::apply()`], but on error it will attach hashes of the input `base_image` and `patch`.
pub fn apply(base_image: &BStr, patch: &Patch<'_, [u8]>) -> Result<BString> {
pub fn apply<S: AsRef<[u8]>>(base_image: S, patch: &Patch<'_, [u8]>) -> Result<BString> {
fn md5_hash_hex(b: impl AsRef<[u8]>) -> String {
format!("{:x}", md5::compute(b))
md5::compute(b).encode_hex()
}
#[derive(Debug)]
@ -4392,7 +4391,7 @@ pub fn apply(base_image: &BStr, patch: &Patch<'_, [u8]>) -> Result<BString> {
}
}
diffy_apply(base_image, patch)
diffy_apply(base_image.as_ref(), patch)
.with_context(|| DebugContext {
base_image_hash: md5_hash_hex(base_image),
hunks: patch.hunks().iter().map(Into::into).collect(),

View File

@ -13,7 +13,7 @@ fn parse_invalid() {
#[test]
fn parse_with_hash() {
let hash = Hunk::hash("hash".as_ref());
let hash = Hunk::hash("hash");
assert_eq!(
format!("2-3-{hash:x}").parse::<Hunk>().unwrap(),
Hunk::new(2, 3, Some(hash), None).unwrap()
@ -43,15 +43,15 @@ fn to_string_no_hash() {
#[test]
fn hash_diff_no_diff_header_is_normal_hash() {
let actual = Hunk::hash_diff("a".as_ref());
let expected = Hunk::hash("a".as_ref());
let actual = Hunk::hash_diff("a");
let expected = Hunk::hash("a");
assert_eq!(actual, expected)
}
#[test]
fn hash_diff_empty_is_fine() {
let actual = Hunk::hash_diff("".as_ref());
let expected = Hunk::hash("".as_ref());
let actual = Hunk::hash_diff("");
let expected = Hunk::hash("");
assert_eq!(
actual, expected,
"The special hash is the same as a normal one in case of empty input.\
@ -61,8 +61,8 @@ fn hash_diff_empty_is_fine() {
#[test]
fn hash_diff_content_hash() {
let a_hash = Hunk::hash_diff("@@x\na".into());
let b_hash = Hunk::hash_diff("@@y\na".into());
let a_hash = Hunk::hash_diff("@@x\na");
let b_hash = Hunk::hash_diff("@@y\na");
assert_eq!(
a_hash, b_hash,
"it skips the first line which is assumed to be a diff-header.\
@ -72,8 +72,8 @@ fn hash_diff_content_hash() {
#[test]
fn eq() {
let a_hash = Hunk::hash("a".as_ref());
let b_hash = Hunk::hash("b".as_ref());
let a_hash = Hunk::hash("a");
let b_hash = Hunk::hash("b");
assert_ne!(a_hash, b_hash);
for (a, b, expected) in vec![
(

View File

@ -16,14 +16,14 @@ fn reconcile_ownership_simple() {
Hunk {
start: 1,
end: 3,
hash: Some(Hunk::hash("1,3".as_ref())),
hash: Some(Hunk::hash("1,3")),
timestamp_ms: None,
locked_to: vec![],
},
Hunk {
start: 4,
end: 6,
hash: Some(Hunk::hash("4,6".as_ref())),
hash: Some(Hunk::hash("4,6")),
timestamp_ms: None,
locked_to: vec![],
},
@ -41,7 +41,7 @@ fn reconcile_ownership_simple() {
hunks: vec![Hunk {
start: 7,
end: 9,
hash: Some(Hunk::hash("7,9".as_ref())),
hash: Some(Hunk::hash("7,9")),
timestamp_ms: None,
locked_to: vec![],
}],
@ -57,14 +57,14 @@ fn reconcile_ownership_simple() {
Hunk {
start: 4,
end: 6,
hash: Some(Hunk::hash("4,6".as_ref())),
hash: Some(Hunk::hash("4,6")),
timestamp_ms: None,
locked_to: vec![],
},
Hunk {
start: 7,
end: 9,
hash: Some(Hunk::hash("9,7".as_ref())),
hash: Some(Hunk::hash("9,7")),
timestamp_ms: None,
locked_to: vec![],
},
@ -83,7 +83,7 @@ fn reconcile_ownership_simple() {
hunks: vec![Hunk {
start: 1,
end: 3,
hash: Some(Hunk::hash("1,3".as_ref())),
hash: Some(Hunk::hash("1,3")),
timestamp_ms: None,
locked_to: vec![],
},],
@ -100,14 +100,14 @@ fn reconcile_ownership_simple() {
Hunk {
start: 4,
end: 6,
hash: Some(Hunk::hash("4,6".as_ref())),
hash: Some(Hunk::hash("4,6")),
timestamp_ms: None,
locked_to: vec![],
},
Hunk {
start: 7,
end: 9,
hash: Some(Hunk::hash("9,7".as_ref())),
hash: Some(Hunk::hash("9,7")),
timestamp_ms: None,
locked_to: vec![],
},

View File

@ -1,7 +1,7 @@
use std::sync::atomic::{AtomicUsize, Ordering};
use anyhow::Result;
use gitbutler_core::virtual_branches::{self, VirtualBranchesHandle};
use gitbutler_core::virtual_branches;
use once_cell::sync::Lazy;
use gitbutler_testsupport::{Case, Suite};
@ -72,7 +72,7 @@ fn empty_iterator() -> Result<()> {
let suite = Suite::default();
let Case { project, .. } = &suite.new_case();
let vb_state = VirtualBranchesHandle::new(&project.gb_dir());
let vb_state = project.virtual_branches();
let iter = vb_state.list_branches()?;
assert_eq!(iter.len(), 0);
@ -85,7 +85,7 @@ fn iterate_all() -> Result<()> {
let suite = Suite::default();
let Case { project, .. } = &suite.new_case();
let vb_state = VirtualBranchesHandle::new(&project.gb_dir());
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())?;

View File

@ -22,7 +22,7 @@ use gitbutler_core::{
errors::CommitError,
integration::verify_branch,
is_remote_branch_mergeable, is_virtual_branch_mergeable, list_remote_branches,
merge_virtual_branch_upstream, unapply_ownership, update_branch, VirtualBranchesHandle,
merge_virtual_branch_upstream, unapply_ownership, update_branch,
},
};
use pretty_assertions::assert_eq;
@ -280,7 +280,7 @@ fn create_branch_with_ownership() -> Result<()> {
virtual_branches::get_status_by_branch(project_repository, None).expect("failed to get status");
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
let vb_state = project_repository.project().virtual_branches();
let branch0 = vb_state.get_branch(&branch0.id).unwrap();
let branch1 = create_virtual_branch(
@ -330,7 +330,7 @@ fn create_branch_in_the_middle() -> Result<()> {
)
.expect("failed to create virtual branch");
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
let vb_state = project_repository.project().virtual_branches();
let mut branches = vb_state.list_branches().expect("failed to read branches");
branches.sort_by_key(|b| b.order);
assert_eq!(branches.len(), 3);
@ -353,7 +353,7 @@ fn create_branch_no_arguments() -> Result<()> {
create_virtual_branch(project_repository, &BranchCreateRequest::default())
.expect("failed to create virtual branch");
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
let vb_state = project_repository.project().virtual_branches();
let branches = vb_state.list_branches().expect("failed to read branches");
assert_eq!(branches.len(), 1);
assert_eq!(branches[0].name, "Virtual branch");
@ -520,7 +520,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 = VirtualBranchesHandle::new(&project.gb_dir());
let vb_state = project.virtual_branches();
let mut branch2 = vb_state.get_branch(&branch2_id)?;
branch2.ownership = BranchOwnershipClaims {
claims: vec!["test.txt:1-5".parse()?],
@ -783,7 +783,7 @@ fn merge_vbranch_upstream_clean_rebase() -> Result<()> {
)?;
set_test_target(project_repository)?;
let vb_state = VirtualBranchesHandle::new(&project.gb_dir());
let vb_state = project.virtual_branches();
vb_state.set_default_target(virtual_branches::target::Target {
branch: "refs/remotes/origin/master".parse().unwrap(),
remote_url: "origin".to_string(),
@ -900,7 +900,7 @@ fn merge_vbranch_upstream_conflict() -> Result<()> {
)?;
set_test_target(project_repository)?;
let vb_state = VirtualBranchesHandle::new(&project.gb_dir());
let vb_state = project.virtual_branches();
vb_state.set_default_target(virtual_branches::target::Target {
branch: "refs/remotes/origin/master".parse().unwrap(),
remote_url: "origin".to_string(),
@ -1303,7 +1303,7 @@ fn detect_mergeable_branch() -> Result<()> {
"line1\nline2\nline3\nline4\nbranch4\n",
)?;
let vb_state = VirtualBranchesHandle::new(&project.gb_dir());
let vb_state = project.virtual_branches();
let mut branch4 = vb_state.get_branch(&branch4_id)?;
branch4.ownership = BranchOwnershipClaims {
@ -1364,7 +1364,7 @@ fn upstream_integrated_vbranch() -> Result<()> {
(PathBuf::from("test3.txt"), "file3\n"),
]));
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
let vb_state = project_repository.project().virtual_branches();
let base_commit = project_repository
.git_repository

View File

@ -18,17 +18,14 @@ pub mod paths {
}
pub mod virtual_branches {
use gitbutler_core::{
project_repository,
virtual_branches::{self, VirtualBranchesHandle},
};
use gitbutler_core::{project_repository, virtual_branches};
use crate::empty_bare_repository;
pub fn set_test_target(
project_repository: &project_repository::Repository,
) -> anyhow::Result<()> {
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
let vb_state = project_repository.project().virtual_branches();
let (remote_repo, _tmp) = empty_bare_repository();
let mut remote = project_repository
.git_repository

View File

@ -9,7 +9,7 @@ use gitbutler_core::projects::ProjectId;
use gitbutler_core::{
deltas::{self, operations::Operation},
reader, sessions,
virtual_branches::{self, branch, VirtualBranchesHandle},
virtual_branches::{self, branch},
};
use once_cell::sync::Lazy;
@ -57,6 +57,7 @@ fn new_test_target() -> virtual_branches::target::Target {
)
.parse()
.unwrap(),
push_remote_name: None,
}
}
@ -659,7 +660,7 @@ fn should_persist_branches_targets_state_between_sessions() -> Result<()> {
..
} = &fixture.new_case_with_files(HashMap::from([(PathBuf::from("test.txt"), "hello world")]));
let vb_state = VirtualBranchesHandle::new(&project.gb_dir());
let vb_state = project.virtual_branches();
let default_target = new_test_target();
vb_state.set_default_target(default_target.clone())?;
@ -675,7 +676,7 @@ fn should_persist_branches_targets_state_between_sessions() -> Result<()> {
std::fs::write(project.path.join("test.txt"), "hello world!").unwrap();
listener.calculate_delta("test.txt", project.id)?;
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
let vb_state = project_repository.project().virtual_branches();
let branches = vb_state.list_branches().unwrap();
assert_eq!(branches.len(), 2);
@ -703,7 +704,7 @@ fn should_restore_branches_targets_state_from_head_session() -> Result<()> {
let Case { project, .. } =
&fixture.new_case_with_files(HashMap::from([(PathBuf::from("test.txt"), "hello world")]));
let vb_state = VirtualBranchesHandle::new(&project.gb_dir());
let vb_state = project.virtual_branches();
let default_target = new_test_target();
vb_state.set_default_target(default_target.clone())?;