move fetch and push to project

This commit is contained in:
Nikita Galaiko 2023-07-03 15:10:31 +02:00
parent 8878c89f06
commit 21ec8d9f14
3 changed files with 48 additions and 42 deletions

View File

@ -467,7 +467,8 @@ impl App {
pub fn push_virtual_branch(&self, project_id: &str, branch_id: &str) -> Result<()> { pub fn push_virtual_branch(&self, project_id: &str, branch_id: &str) -> Result<()> {
let gb_repository = self.gb_repository(project_id)?; let gb_repository = self.gb_repository(project_id)?;
let project = self.gb_project(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(()) Ok(())
} }

View File

@ -1,4 +1,4 @@
use std::{collections::HashMap, env}; use std::{collections::HashMap, env, process::Command};
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use git2::Diff; use git2::Diff;
@ -359,6 +359,39 @@ impl<'repository> Repository<'repository> {
Ok(()) 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<()> { pub fn git_commit(&self, message: &str, push: bool) -> Result<()> {
let config = self let config = self
.git_repository .git_repository

View File

@ -1700,12 +1700,11 @@ fn name_to_branch(name: &str) -> String {
.map(|c| if c.is_ascii_alphanumeric() { c } else { '-' }) .map(|c| if c.is_ascii_alphanumeric() { c } else { '-' })
.collect::<String>(); .collect::<String>();
return format!("refs/heads/{}", cleaned_name); format!("refs/heads/{}", cleaned_name)
} }
use std::process::Command;
pub fn push( pub fn push(
project_path: &str, project_repository: &project_repository::Repository,
gb_repository: &gb_repository::Repository, gb_repository: &gb_repository::Repository,
branch_id: &str, branch_id: &str,
) -> Result<()> { ) -> Result<()> {
@ -1728,45 +1727,18 @@ pub fn push(
vbranch.upstream.clone() vbranch.upstream.clone()
}; };
let output = Command::new("git") project_repository
.arg("push") .push(&vbranch.head, &upstream)
.arg("origin") .context("failed to fetch before push")?;
.arg(format!("{}:{}", vbranch.head, upstream))
.current_dir(project_path)
.output()
.context("failed to fork exec")?;
if output.status.success() { vbranch.upstream = upstream;
vbranch.upstream = upstream; branch_writer
branch_writer .write(&vbranch)
.write(&vbranch) .context("failed to write target branch after push")?;
.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)?
))
}
}
fn fetch(project_path: &str) -> Result<()> { project_repository
let output = Command::new("git") .fetch()
.arg("fetch") .context("failed to fetch before push")
.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)?
))
}
} }
#[cfg(test)] #[cfg(test)]