diff --git a/src-tauri/src/app.rs b/src-tauri/src/app.rs index b738ed174..4c7004f95 100644 --- a/src-tauri/src/app.rs +++ b/src-tauri/src/app.rs @@ -467,7 +467,8 @@ impl App { pub fn push_virtual_branch(&self, project_id: &str, branch_id: &str) -> Result<()> { let gb_repository = self.gb_repository(project_id)?; let project = self.gb_project(project_id)?; - virtual_branches::push(&project.path, &gb_repository, branch_id)?; + let project_repository = project_repository::Repository::open(&project)?; + virtual_branches::push(&project_repository, &gb_repository, branch_id)?; Ok(()) } diff --git a/src-tauri/src/project_repository/repository.rs b/src-tauri/src/project_repository/repository.rs index 21cb27f6a..c104fb8cc 100644 --- a/src-tauri/src/project_repository/repository.rs +++ b/src-tauri/src/project_repository/repository.rs @@ -1,4 +1,4 @@ -use std::{collections::HashMap, env}; +use std::{collections::HashMap, env, process::Command}; use anyhow::{Context, Result}; use git2::Diff; @@ -359,6 +359,39 @@ impl<'repository> Repository<'repository> { Ok(()) } + pub fn push(&self, head: &git2::Oid, upstream: &str) -> Result<()> { + let output = Command::new("git") + .arg("push") + .arg("origin") + .arg(format!("{}:{}", head, upstream)) + .current_dir(&self.project.path) + .output() + .context("failed to fork exec")?; + + output.status.success().then(|| ()).ok_or_else(|| { + anyhow::anyhow!( + "failed to push: {}", + String::from_utf8(output.stderr).unwrap() + ) + }) + } + + pub fn fetch(&self) -> Result<()> { + let output = Command::new("git") + .arg("fetch") + .arg("origin") + .current_dir(&self.project.path) + .output() + .context("failed to fork exec")?; + + output.status.success().then(|| ()).ok_or_else(|| { + anyhow::anyhow!( + "failed to fetch: {}", + String::from_utf8(output.stderr).unwrap() + ) + }) + } + pub fn git_commit(&self, message: &str, push: bool) -> Result<()> { let config = self .git_repository diff --git a/src-tauri/src/virtual_branches/mod.rs b/src-tauri/src/virtual_branches/mod.rs index cb591e795..d70573915 100644 --- a/src-tauri/src/virtual_branches/mod.rs +++ b/src-tauri/src/virtual_branches/mod.rs @@ -1700,12 +1700,11 @@ fn name_to_branch(name: &str) -> String { .map(|c| if c.is_ascii_alphanumeric() { c } else { '-' }) .collect::(); - return format!("refs/heads/{}", cleaned_name); + format!("refs/heads/{}", cleaned_name) } -use std::process::Command; pub fn push( - project_path: &str, + project_repository: &project_repository::Repository, gb_repository: &gb_repository::Repository, branch_id: &str, ) -> Result<()> { @@ -1728,45 +1727,18 @@ pub fn push( vbranch.upstream.clone() }; - let output = Command::new("git") - .arg("push") - .arg("origin") - .arg(format!("{}:{}", vbranch.head, upstream)) - .current_dir(project_path) - .output() - .context("failed to fork exec")?; + project_repository + .push(&vbranch.head, &upstream) + .context("failed to fetch before push")?; - if output.status.success() { - vbranch.upstream = upstream; - branch_writer - .write(&vbranch) - .context("failed to write target branch after push")?; - fetch(project_path).context("failed to fetch after push")?; - Ok(()) - } else { - Err(anyhow::anyhow!( - "failed to push branch: {}", - String::from_utf8(output.stderr)? - )) - } -} + vbranch.upstream = upstream; + branch_writer + .write(&vbranch) + .context("failed to write target branch after push")?; -fn fetch(project_path: &str) -> Result<()> { - let output = Command::new("git") - .arg("fetch") - .arg("origin") - .current_dir(project_path) - .output() - .context("failed to fork exec")?; - - if output.status.success() { - Ok(()) - } else { - Err(anyhow::anyhow!( - "failed to fetch: {}", - String::from_utf8(output.stderr)? - )) - } + project_repository + .fetch() + .context("failed to fetch before push") } #[cfg(test)]