mirror of
https://github.com/gitbutlerapp/gitbutler.git
synced 2024-12-20 08:01:46 +03:00
Merge pull request #3668 from gitbutlerapp/simplify-default-target
simplify default target retrieval logic
This commit is contained in:
commit
e3263774f6
crates/gitbutler-core/src/virtual_branches
@ -55,6 +55,19 @@ impl VirtualBranchesHandle {
|
|||||||
.ok_or(crate::reader::Error::NotFound)
|
.ok_or(crate::reader::Error::NotFound)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Attempts to get the default target for the given repository,
|
||||||
|
/// returning None if it's not found.
|
||||||
|
///
|
||||||
|
/// Errors if the file cannot be read or written.
|
||||||
|
#[inline]
|
||||||
|
pub fn try_get_default_target(&self) -> Result<Option<Target>, crate::reader::Error> {
|
||||||
|
match self.get_default_target() {
|
||||||
|
Ok(target) => Ok(Some(target)),
|
||||||
|
Err(crate::reader::Error::NotFound) => Ok(None),
|
||||||
|
Err(e) => Err(e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Sets the target for the given virtual branch.
|
/// Sets the target for the given virtual branch.
|
||||||
///
|
///
|
||||||
/// Errors if the file cannot be read or written.
|
/// Errors if the file cannot be read or written.
|
||||||
|
@ -210,16 +210,6 @@ pub fn normalize_branch_name(name: &str) -> String {
|
|||||||
pattern.replace_all(name, "-").to_string()
|
pattern.replace_all(name, "-").to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_default_target(
|
|
||||||
vb_state: &VirtualBranchesHandle,
|
|
||||||
) -> Result<Option<target::Target>, reader::Error> {
|
|
||||||
match vb_state.get_default_target() {
|
|
||||||
Ok(target) => Ok(Some(target)),
|
|
||||||
Err(reader::Error::NotFound) => Ok(None),
|
|
||||||
Err(error) => Err(error),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn apply_branch(
|
pub fn apply_branch(
|
||||||
project_repository: &project_repository::Repository,
|
project_repository: &project_repository::Repository,
|
||||||
branch_id: &BranchId,
|
branch_id: &BranchId,
|
||||||
@ -236,13 +226,16 @@ pub fn apply_branch(
|
|||||||
let repo = &project_repository.git_repository;
|
let repo = &project_repository.git_repository;
|
||||||
|
|
||||||
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
|
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
|
||||||
let default_target = get_default_target(&vb_state)
|
let Some(default_target) = vb_state
|
||||||
|
.try_get_default_target()
|
||||||
.context("failed to get default target")?
|
.context("failed to get default target")?
|
||||||
.ok_or_else(|| {
|
else {
|
||||||
errors::ApplyBranchError::DefaultTargetNotSet(errors::DefaultTargetNotSet {
|
return Err(errors::ApplyBranchError::DefaultTargetNotSet(
|
||||||
|
errors::DefaultTargetNotSet {
|
||||||
project_id: project_repository.project().id,
|
project_id: project_repository.project().id,
|
||||||
})
|
},
|
||||||
})?;
|
));
|
||||||
|
};
|
||||||
|
|
||||||
let mut branch = match vb_state.get_branch(branch_id) {
|
let mut branch = match vb_state.get_branch(branch_id) {
|
||||||
Ok(branch) => Ok(branch),
|
Ok(branch) => Ok(branch),
|
||||||
@ -489,13 +482,16 @@ pub fn unapply_ownership(
|
|||||||
|
|
||||||
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
|
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
|
||||||
|
|
||||||
let default_target = get_default_target(&vb_state)
|
let Some(default_target) = vb_state
|
||||||
|
.try_get_default_target()
|
||||||
.context("failed to get default target")?
|
.context("failed to get default target")?
|
||||||
.ok_or_else(|| {
|
else {
|
||||||
errors::UnapplyOwnershipError::DefaultTargetNotSet(errors::DefaultTargetNotSet {
|
return Err(errors::UnapplyOwnershipError::DefaultTargetNotSet(
|
||||||
|
errors::DefaultTargetNotSet {
|
||||||
project_id: project_repository.project().id,
|
project_id: project_repository.project().id,
|
||||||
})
|
},
|
||||||
})?;
|
));
|
||||||
|
};
|
||||||
|
|
||||||
let applied_branches = vb_state
|
let applied_branches = vb_state
|
||||||
.list_branches()
|
.list_branches()
|
||||||
@ -649,13 +645,16 @@ pub fn unapply_branch(
|
|||||||
return Ok(Some(target_branch));
|
return Ok(Some(target_branch));
|
||||||
}
|
}
|
||||||
|
|
||||||
let default_target = get_default_target(&vb_state)
|
let Some(default_target) = vb_state
|
||||||
|
.try_get_default_target()
|
||||||
.context("failed to get default target")?
|
.context("failed to get default target")?
|
||||||
.ok_or_else(|| {
|
else {
|
||||||
errors::UnapplyBranchError::DefaultTargetNotSet(errors::DefaultTargetNotSet {
|
return Err(errors::UnapplyBranchError::DefaultTargetNotSet(
|
||||||
|
errors::DefaultTargetNotSet {
|
||||||
project_id: project_repository.project().id,
|
project_id: project_repository.project().id,
|
||||||
})
|
},
|
||||||
})?;
|
));
|
||||||
|
};
|
||||||
|
|
||||||
let repo = &project_repository.git_repository;
|
let repo = &project_repository.git_repository;
|
||||||
let target_commit = repo
|
let target_commit = repo
|
||||||
@ -1059,13 +1058,16 @@ pub fn create_virtual_branch(
|
|||||||
) -> Result<branch::Branch, errors::CreateVirtualBranchError> {
|
) -> Result<branch::Branch, errors::CreateVirtualBranchError> {
|
||||||
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
|
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
|
||||||
|
|
||||||
let default_target = get_default_target(&vb_state)
|
let Some(default_target) = vb_state
|
||||||
|
.try_get_default_target()
|
||||||
.context("failed to get default target")?
|
.context("failed to get default target")?
|
||||||
.ok_or_else(|| {
|
else {
|
||||||
errors::CreateVirtualBranchError::DefaultTargetNotSet(errors::DefaultTargetNotSet {
|
return Err(errors::CreateVirtualBranchError::DefaultTargetNotSet(
|
||||||
|
errors::DefaultTargetNotSet {
|
||||||
project_id: project_repository.project().id,
|
project_id: project_repository.project().id,
|
||||||
})
|
},
|
||||||
})?;
|
));
|
||||||
|
};
|
||||||
|
|
||||||
let commit = project_repository
|
let commit = project_repository
|
||||||
.git_repository
|
.git_repository
|
||||||
@ -1416,13 +1418,17 @@ pub fn update_branch(
|
|||||||
};
|
};
|
||||||
|
|
||||||
if let Some(updated_upstream) = branch_update.upstream {
|
if let Some(updated_upstream) = branch_update.upstream {
|
||||||
let default_target = get_default_target(&vb_state)
|
let Some(default_target) = vb_state
|
||||||
|
.try_get_default_target()
|
||||||
.context("failed to get default target")?
|
.context("failed to get default target")?
|
||||||
.ok_or_else(|| {
|
else {
|
||||||
errors::UpdateBranchError::DefaultTargetNotSet(errors::DefaultTargetNotSet {
|
return Err(errors::UpdateBranchError::DefaultTargetNotSet(
|
||||||
|
errors::DefaultTargetNotSet {
|
||||||
project_id: project_repository.project().id,
|
project_id: project_repository.project().id,
|
||||||
})
|
},
|
||||||
})?;
|
));
|
||||||
|
};
|
||||||
|
|
||||||
let remote_branch = format!(
|
let remote_branch = format!(
|
||||||
"refs/remotes/{}/{}",
|
"refs/remotes/{}/{}",
|
||||||
default_target.branch.remote(),
|
default_target.branch.remote(),
|
||||||
@ -1617,13 +1623,12 @@ pub fn get_status_by_branch(
|
|||||||
) -> Result<(AppliedStatuses, Vec<diff::FileDiff>)> {
|
) -> Result<(AppliedStatuses, Vec<diff::FileDiff>)> {
|
||||||
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
|
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
|
||||||
|
|
||||||
let default_target =
|
let Some(default_target) = vb_state
|
||||||
match get_default_target(&vb_state).context("failed to read default target")? {
|
.try_get_default_target()
|
||||||
Some(target) => target,
|
.context("failed to read default target")?
|
||||||
None => {
|
else {
|
||||||
return Ok((vec![], vec![]));
|
return Ok((vec![], vec![]));
|
||||||
}
|
};
|
||||||
};
|
|
||||||
|
|
||||||
let virtual_branches = vb_state
|
let virtual_branches = vb_state
|
||||||
.list_branches()
|
.list_branches()
|
||||||
@ -2003,13 +2008,16 @@ pub fn reset_branch(
|
|||||||
) -> Result<(), errors::ResetBranchError> {
|
) -> Result<(), errors::ResetBranchError> {
|
||||||
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
|
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
|
||||||
|
|
||||||
let default_target = get_default_target(&vb_state)
|
let Some(default_target) = vb_state
|
||||||
|
.try_get_default_target()
|
||||||
.context("failed to read default target")?
|
.context("failed to read default target")?
|
||||||
.ok_or_else(|| {
|
else {
|
||||||
errors::ResetBranchError::DefaultTargetNotSet(errors::DefaultTargetNotSet {
|
return Err(errors::ResetBranchError::DefaultTargetNotSet(
|
||||||
|
errors::DefaultTargetNotSet {
|
||||||
project_id: project_repository.project().id,
|
project_id: project_repository.project().id,
|
||||||
})
|
},
|
||||||
})?;
|
));
|
||||||
|
};
|
||||||
|
|
||||||
let mut branch = match vb_state.get_branch(branch_id) {
|
let mut branch = match vb_state.get_branch(branch_id) {
|
||||||
Ok(branch) => Ok(branch),
|
Ok(branch) => Ok(branch),
|
||||||
@ -2439,13 +2447,16 @@ pub fn push(
|
|||||||
let remote_branch = if let Some(upstream_branch) = vbranch.upstream.as_ref() {
|
let remote_branch = if let Some(upstream_branch) = vbranch.upstream.as_ref() {
|
||||||
upstream_branch.clone()
|
upstream_branch.clone()
|
||||||
} else {
|
} else {
|
||||||
let default_target = get_default_target(&vb_state)
|
let Some(default_target) = vb_state
|
||||||
|
.try_get_default_target()
|
||||||
.context("failed to get default target")?
|
.context("failed to get default target")?
|
||||||
.ok_or_else(|| {
|
else {
|
||||||
errors::PushError::DefaultTargetNotSet(errors::DefaultTargetNotSet {
|
return Err(errors::PushError::DefaultTargetNotSet(
|
||||||
|
errors::DefaultTargetNotSet {
|
||||||
project_id: project_repository.project().id,
|
project_id: project_repository.project().id,
|
||||||
})
|
},
|
||||||
})?;
|
));
|
||||||
|
};
|
||||||
|
|
||||||
let remote_branch = format!(
|
let remote_branch = format!(
|
||||||
"refs/remotes/{}/{}",
|
"refs/remotes/{}/{}",
|
||||||
@ -2571,13 +2582,16 @@ pub fn is_remote_branch_mergeable(
|
|||||||
) -> Result<bool, errors::IsRemoteBranchMergableError> {
|
) -> Result<bool, errors::IsRemoteBranchMergableError> {
|
||||||
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
|
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
|
||||||
|
|
||||||
let default_target = get_default_target(&vb_state)
|
let Some(default_target) = vb_state
|
||||||
|
.try_get_default_target()
|
||||||
.context("failed to get default target")?
|
.context("failed to get default target")?
|
||||||
.ok_or_else(|| {
|
else {
|
||||||
errors::IsRemoteBranchMergableError::DefaultTargetNotSet(errors::DefaultTargetNotSet {
|
return Err(errors::IsRemoteBranchMergableError::DefaultTargetNotSet(
|
||||||
|
errors::DefaultTargetNotSet {
|
||||||
project_id: project_repository.project().id,
|
project_id: project_repository.project().id,
|
||||||
})
|
},
|
||||||
})?;
|
));
|
||||||
|
};
|
||||||
|
|
||||||
let target_commit = project_repository
|
let target_commit = project_repository
|
||||||
.git_repository
|
.git_repository
|
||||||
@ -2638,13 +2652,16 @@ pub fn is_virtual_branch_mergeable(
|
|||||||
return Ok(true);
|
return Ok(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
let default_target = get_default_target(&vb_state)
|
let Some(default_target) = vb_state
|
||||||
|
.try_get_default_target()
|
||||||
.context("failed to read default target")?
|
.context("failed to read default target")?
|
||||||
.ok_or_else(|| {
|
else {
|
||||||
errors::IsVirtualBranchMergeable::DefaultTargetNotSet(errors::DefaultTargetNotSet {
|
return Err(errors::IsVirtualBranchMergeable::DefaultTargetNotSet(
|
||||||
|
errors::DefaultTargetNotSet {
|
||||||
project_id: project_repository.project().id,
|
project_id: project_repository.project().id,
|
||||||
})
|
},
|
||||||
})?;
|
));
|
||||||
|
};
|
||||||
|
|
||||||
// determine if this branch is up to date with the target/base
|
// determine if this branch is up to date with the target/base
|
||||||
let merge_base = project_repository
|
let merge_base = project_repository
|
||||||
@ -2712,13 +2729,16 @@ pub fn move_commit_file(
|
|||||||
}
|
}
|
||||||
.context("failed to read branch")?;
|
.context("failed to read branch")?;
|
||||||
|
|
||||||
let default_target = get_default_target(&vb_state)
|
let Some(default_target) = vb_state
|
||||||
|
.try_get_default_target()
|
||||||
.context("failed to read default target")?
|
.context("failed to read default target")?
|
||||||
.ok_or_else(|| {
|
else {
|
||||||
errors::VirtualBranchError::DefaultTargetNotSet(errors::DefaultTargetNotSet {
|
return Err(errors::VirtualBranchError::DefaultTargetNotSet(
|
||||||
|
errors::DefaultTargetNotSet {
|
||||||
project_id: project_repository.project().id,
|
project_id: project_repository.project().id,
|
||||||
})
|
},
|
||||||
})?;
|
));
|
||||||
|
};
|
||||||
|
|
||||||
let mut to_amend_oid = to_commit_oid;
|
let mut to_amend_oid = to_commit_oid;
|
||||||
let mut amend_commit = project_repository
|
let mut amend_commit = project_repository
|
||||||
@ -3001,13 +3021,16 @@ pub fn amend(
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let default_target = get_default_target(&vb_state)
|
let Some(default_target) = vb_state
|
||||||
|
.try_get_default_target()
|
||||||
.context("failed to read default target")?
|
.context("failed to read default target")?
|
||||||
.ok_or_else(|| {
|
else {
|
||||||
errors::VirtualBranchError::DefaultTargetNotSet(errors::DefaultTargetNotSet {
|
return Err(errors::VirtualBranchError::DefaultTargetNotSet(
|
||||||
|
errors::DefaultTargetNotSet {
|
||||||
project_id: project_repository.project().id,
|
project_id: project_repository.project().id,
|
||||||
})
|
},
|
||||||
})?;
|
));
|
||||||
|
};
|
||||||
|
|
||||||
let integration_commit_id =
|
let integration_commit_id =
|
||||||
super::integration::get_workspace_head(&vb_state, project_repository)?;
|
super::integration::get_workspace_head(&vb_state, project_repository)?;
|
||||||
@ -3154,13 +3177,16 @@ pub fn reorder_commit(
|
|||||||
) -> Result<(), errors::VirtualBranchError> {
|
) -> Result<(), errors::VirtualBranchError> {
|
||||||
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
|
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
|
||||||
|
|
||||||
let default_target = get_default_target(&vb_state)
|
let Some(default_target) = vb_state
|
||||||
|
.try_get_default_target()
|
||||||
.context("failed to read default target")?
|
.context("failed to read default target")?
|
||||||
.ok_or_else(|| {
|
else {
|
||||||
errors::VirtualBranchError::DefaultTargetNotSet(errors::DefaultTargetNotSet {
|
return Err(errors::VirtualBranchError::DefaultTargetNotSet(
|
||||||
|
errors::DefaultTargetNotSet {
|
||||||
project_id: project_repository.project().id,
|
project_id: project_repository.project().id,
|
||||||
})
|
},
|
||||||
})?;
|
));
|
||||||
|
};
|
||||||
|
|
||||||
let mut branch = match vb_state.get_branch(branch_id) {
|
let mut branch = match vb_state.get_branch(branch_id) {
|
||||||
Ok(branch) => Ok(branch),
|
Ok(branch) => Ok(branch),
|
||||||
@ -3563,7 +3589,8 @@ pub fn cherry_pick(
|
|||||||
.find_commit(branch.head)
|
.find_commit(branch.head)
|
||||||
.context("failed to find branch tree")?;
|
.context("failed to find branch tree")?;
|
||||||
|
|
||||||
let default_target = get_default_target(&vb_state)
|
let default_target = vb_state
|
||||||
|
.try_get_default_target()
|
||||||
.context("failed to read default target")?
|
.context("failed to read default target")?
|
||||||
.context("no default target set")?;
|
.context("no default target set")?;
|
||||||
|
|
||||||
@ -3723,13 +3750,16 @@ pub fn squash(
|
|||||||
|
|
||||||
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
|
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
|
||||||
|
|
||||||
let default_target = get_default_target(&vb_state)
|
let Some(default_target) = vb_state
|
||||||
|
.try_get_default_target()
|
||||||
.context("failed to read default target")?
|
.context("failed to read default target")?
|
||||||
.ok_or_else(|| {
|
else {
|
||||||
errors::SquashError::DefaultTargetNotSet(errors::DefaultTargetNotSet {
|
return Err(errors::SquashError::DefaultTargetNotSet(
|
||||||
|
errors::DefaultTargetNotSet {
|
||||||
project_id: project_repository.project().id,
|
project_id: project_repository.project().id,
|
||||||
})
|
},
|
||||||
})?;
|
));
|
||||||
|
};
|
||||||
|
|
||||||
let mut branch = vb_state
|
let mut branch = vb_state
|
||||||
.get_branch(branch_id)
|
.get_branch(branch_id)
|
||||||
@ -3856,13 +3886,16 @@ pub fn update_commit_message(
|
|||||||
|
|
||||||
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
|
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
|
||||||
|
|
||||||
let default_target = get_default_target(&vb_state)
|
let Some(default_target) = vb_state
|
||||||
|
.try_get_default_target()
|
||||||
.context("failed to read default target")?
|
.context("failed to read default target")?
|
||||||
.ok_or_else(|| {
|
else {
|
||||||
errors::UpdateCommitMessageError::DefaultTargetNotSet(errors::DefaultTargetNotSet {
|
return Err(errors::UpdateCommitMessageError::DefaultTargetNotSet(
|
||||||
|
errors::DefaultTargetNotSet {
|
||||||
project_id: project_repository.project().id,
|
project_id: project_repository.project().id,
|
||||||
})
|
},
|
||||||
})?;
|
));
|
||||||
|
};
|
||||||
|
|
||||||
let mut branch = vb_state
|
let mut branch = vb_state
|
||||||
.get_branch(branch_id)
|
.get_branch(branch_id)
|
||||||
@ -3987,13 +4020,16 @@ pub fn move_commit(
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let default_target = get_default_target(&vb_state)
|
let Some(default_target) = vb_state
|
||||||
|
.try_get_default_target()
|
||||||
.context("failed to get default target")?
|
.context("failed to get default target")?
|
||||||
.ok_or_else(|| {
|
else {
|
||||||
errors::MoveCommitError::DefaultTargetNotSet(errors::DefaultTargetNotSet {
|
return Err(errors::MoveCommitError::DefaultTargetNotSet(
|
||||||
|
errors::DefaultTargetNotSet {
|
||||||
project_id: project_repository.project().id,
|
project_id: project_repository.project().id,
|
||||||
})
|
},
|
||||||
})?;
|
));
|
||||||
|
};
|
||||||
|
|
||||||
let integration_commit_id =
|
let integration_commit_id =
|
||||||
super::integration::get_workspace_head(&vb_state, project_repository)?;
|
super::integration::get_workspace_head(&vb_state, project_repository)?;
|
||||||
@ -4140,15 +4176,18 @@ pub fn create_virtual_branch_from_branch(
|
|||||||
|
|
||||||
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
|
let vb_state = VirtualBranchesHandle::new(&project_repository.project().gb_dir());
|
||||||
|
|
||||||
let default_target = get_default_target(&vb_state)
|
let Some(default_target) = vb_state
|
||||||
|
.try_get_default_target()
|
||||||
.context("failed to get default target")?
|
.context("failed to get default target")?
|
||||||
.ok_or_else(|| {
|
else {
|
||||||
|
return Err(
|
||||||
errors::CreateVirtualBranchFromBranchError::DefaultTargetNotSet(
|
errors::CreateVirtualBranchFromBranchError::DefaultTargetNotSet(
|
||||||
errors::DefaultTargetNotSet {
|
errors::DefaultTargetNotSet {
|
||||||
project_id: project_repository.project().id,
|
project_id: project_repository.project().id,
|
||||||
},
|
},
|
||||||
)
|
),
|
||||||
})?;
|
);
|
||||||
|
};
|
||||||
|
|
||||||
if let git::Refname::Remote(remote_upstream) = upstream {
|
if let git::Refname::Remote(remote_upstream) = upstream {
|
||||||
if default_target.branch.eq(remote_upstream) {
|
if default_target.branch.eq(remote_upstream) {
|
||||||
|
Loading…
Reference in New Issue
Block a user