Update repo using git fetch

This commit is contained in:
Joscha 2023-08-17 01:15:46 +02:00
parent 0a9559f82a
commit 1faf42bd82
2 changed files with 57 additions and 5 deletions

View file

@ -0,0 +1,47 @@
//! Update repo refs using the `git` binary.
use std::process::Command;
use gix::bstr::ByteVec;
use tracing::{info, warn};
use crate::{config::ServerConfig, server::Repo, somehow};
fn fetch(repo: Repo, url: &str, refspecs: &[String]) -> somehow::Result<()> {
info!("Fetching refs from {url}");
let mut command = Command::new("git");
command
.arg("fetch")
.arg("-C")
.arg(repo.0.path())
.arg("--prune")
.arg("--")
.arg(url);
for refspec in refspecs {
command.arg(refspec);
}
let output = command.output()?;
if output.status.success() {
} else {
warn!(exitcode = %output.status, "'git fetch' failed");
warn!(output = "stdout", "{}", output.stdout.into_string_lossy());
warn!(output = "stderr", "{}", output.stderr.into_string_lossy());
}
Ok(())
}
async fn inner(repo: Repo, url: &'static str, refspecs: &'static [String]) -> somehow::Result<()> {
tokio::task::spawn_blocking(move || fetch(repo, url, refspecs)).await??;
Ok(())
}
pub async fn update(config: &'static ServerConfig, repo: Repo) {
if let Some(url) = &config.repo_fetch_url {
if let Err(e) = inner(repo, url, &config.repo_fetch_refspecs).await {
warn!("Error fetching refs:\n{e:?}");
}
}
}