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<()> {
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(())
}

View File

@ -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

View File

@ -1700,12 +1700,11 @@ fn name_to_branch(name: &str) -> String {
.map(|c| if c.is_ascii_alphanumeric() { c } else { '-' })
.collect::<String>();
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)]