small fixes

This commit is contained in:
Nikita Galaiko 2023-08-31 15:35:08 +02:00 committed by GitButler
parent b06cce901d
commit eecbbd4073
11 changed files with 39 additions and 28 deletions

View File

@ -272,7 +272,7 @@ impl App {
.map_err(|e| Error::Other(e.into()))?;
project_repository
.fetch(&default_target.branch.remote(), &key)
.fetch(default_target.branch.remote(), &key)
.map_err(Error::FetchError)
}

View File

@ -9,7 +9,7 @@ pub enum Error {
#[error("branch is not remote: {0}")]
NotRemote(String),
#[error(transparent)]
GitError(#[from] git::Error),
Git(#[from] git::Error),
#[error(transparent)]
Utf8Error(#[from] std::string::FromUtf8Error),
Utf8(#[from] std::string::FromUtf8Error),
}

View File

@ -66,7 +66,7 @@ impl TryFrom<&git::Branch<'_>> for Name {
type Error = Error;
fn try_from(value: &git::Branch<'_>) -> std::result::Result<Self, Self::Error> {
let branch = String::from_utf8(value.refname_bytes().to_vec()).map_err(Error::Utf8Error)?;
let branch = String::from_utf8(value.refname_bytes().to_vec()).map_err(Error::Utf8)?;
if value.is_remote() {
Err(Error::NotLocal(branch))
} else {

View File

@ -82,8 +82,7 @@ impl TryFrom<&git::Branch<'_>> for Name {
type Error = Error;
fn try_from(value: &git::Branch<'_>) -> std::result::Result<Self, Self::Error> {
let refname =
String::from_utf8(value.refname_bytes().to_vec()).map_err(Error::Utf8Error)?;
let refname = String::from_utf8(value.refname_bytes().to_vec()).map_err(Error::Utf8)?;
if !value.is_remote() {
return Err(Error::NotRemote(refname));

View File

@ -423,10 +423,13 @@ async fn git_head(handle: tauri::AppHandle, project_id: &str) -> Result<String,
async fn git_switch_branch(
handle: tauri::AppHandle,
project_id: &str,
branch: git::LocalBranchName,
branch: &str,
) -> Result<(), Error> {
let app = handle.state::<app::App>();
app.git_switch_branch(project_id, &branch)
let branch_name = format!("refs/heads/{}", branch)
.parse()
.context("invalid branch name")?;
app.git_switch_branch(project_id, &branch_name)
.with_context(|| format!("failed to switch git branch for project {}", project_id))?;
Ok(())
}

View File

@ -1,3 +1,4 @@
use anyhow::Context;
use tauri::{AppHandle, Manager};
use tracing::instrument;
@ -79,11 +80,14 @@ pub async fn get_base_branch_data(
pub async fn set_base_branch(
handle: AppHandle,
project_id: &str,
branch: git::RemoteBranchName,
branch: &str,
) -> Result<super::BaseBranch, Error> {
let branch_name = format!("refs/remotes/{}", branch)
.parse()
.context("Invalid branch name")?;
handle
.state::<Controller>()
.set_base_branch(project_id, &branch)
.set_base_branch(project_id, &branch_name)
.await
.map_err(Into::into)
}

View File

@ -92,7 +92,9 @@ mod tests {
"refs/remotes/branch name{}/remote name {}",
unsafe { TEST_TARGET_INDEX },
unsafe { TEST_TARGET_INDEX }
).parse().unwrap(),
)
.parse()
.unwrap(),
remote_url: format!("remote url {}", unsafe { TEST_TARGET_INDEX }),
sha: format!("0123456789abcdef0123456789abcdef0123456{}", unsafe {
TEST_TARGET_INDEX

View File

@ -62,7 +62,7 @@ impl TryFrom<&dyn crate::reader::Reader> for Target {
type Error = crate::reader::Error;
fn try_from(reader: &dyn crate::reader::Reader) -> Result<Self, Self::Error> {
let (remote_name, branch_name) = read_remote_name_branch_name(reader).map_err(|e| {
let (_, branch_name) = read_remote_name_branch_name(reader).map_err(|e| {
crate::reader::Error::IOError(std::io::Error::new(
std::io::ErrorKind::Other,
format!("branch: {}", e),
@ -91,7 +91,7 @@ impl TryFrom<&dyn crate::reader::Reader> for Target {
})?;
Ok(Self {
branch: git::RemoteBranchName::new(&remote_name, &branch_name),
branch: format!("refs/remotes/{}", branch_name).parse().unwrap(),
remote_url,
sha,
})

View File

@ -138,7 +138,7 @@ mod tests {
let reader = TargetReader::new(&session_reader);
let read = reader.read_default().unwrap();
assert_eq!(read.branch.branch(), "origin/master");
assert_eq!(read.branch.branch(), "master");
assert_eq!(read.branch.remote(), "origin");
assert_eq!(
read.remote_url,

View File

@ -28,10 +28,13 @@ impl<'writer> TargetWriter<'writer> {
let _lock = self.repository.lock();
self.writer
.write_string("branches/target/branch_name", &target.branch.branch())
.write_string(
"branches/target/branch_name",
&format!("{}/{}", target.branch.remote(), target.branch.branch()),
)
.context("Failed to write default target branch name")?;
self.writer
.write_string("branches/target/remote_name", &target.branch.remote())
.write_string("branches/target/remote_name", target.branch.remote())
.context("Failed to write default target remote name ")?;
self.writer
.write_string("branches/target/remote_url", &target.remote_url)
@ -52,13 +55,13 @@ impl<'writer> TargetWriter<'writer> {
self.writer
.write_string(
&format!("branches/{}/target/branch_name", id),
&target.branch.branch(),
&format!("{}/{}", target.branch.remote(), target.branch.branch()),
)
.context("Failed to write branch target branch name")?;
self.writer
.write_string(
&format!("branches/{}/target/remote_name", id),
&target.branch.remote(),
target.branch.remote(),
)
.context("Failed to write branch target remote")?;
self.writer
@ -138,9 +141,7 @@ mod tests {
let branch = test_branch();
let target = Target {
branch: format!("refs/remotes/remote name/branch name")
.parse()
.unwrap(),
branch: "refs/remotes/remote name/branch name".parse().unwrap(),
remote_url: "remote url".to_string(),
sha: "0123456789abcdef0123456789abcdef01234567".parse().unwrap(),
};
@ -161,7 +162,7 @@ mod tests {
assert_eq!(
fs::read_to_string(root.join("target").join("branch_name").to_str().unwrap())
.context("Failed to read branch target name")?,
target.branch.branch()
format!("{}/{}", target.branch.remote(), target.branch.branch())
);
assert_eq!(
fs::read_to_string(root.join("target").join("remote_name").to_str().unwrap())
@ -231,9 +232,7 @@ mod tests {
let branch = test_branch();
let target = Target {
branch: format!("refs/remotes/remote name/branch name")
.parse()
.unwrap(),
branch: "refs/remotes/remote name/branch name".parse().unwrap(),
remote_url: "remote url".to_string(),
sha: "0123456789abcdef0123456789abcdef01234567".parse().unwrap(),
};
@ -244,7 +243,7 @@ mod tests {
target_writer.write(&branch.id, &target)?;
let updated_target = Target {
branch: format!("refs/remotes/updated remote name/updated branch name")
branch: "refs/remotes/updated remote name/updated branch name"
.parse()
.unwrap(),
remote_url: "updated remote url".to_string(),
@ -258,7 +257,11 @@ mod tests {
assert_eq!(
fs::read_to_string(root.join("target").join("branch_name").to_str().unwrap())
.context("Failed to read branch target branch name")?,
updated_target.branch.branch()
format!(
"{}/{}",
updated_target.branch.remote(),
updated_target.branch.branch()
)
);
assert_eq!(

View File

@ -102,7 +102,7 @@ impl HandlerInner {
let project_repository = project_repository::Repository::open(&project)?;
let fetch_result =
if let Err(err) = project_repository.fetch(&default_target.branch.remote(), &key) {
if let Err(err) = project_repository.fetch(default_target.branch.remote(), &key) {
tracing::error!("{}: failed to fetch project data: {:#}", project_id, err);
projects::FetchResult::Error {
attempt: project