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

@ -1,16 +1,18 @@
//! Recurring actions and updates. //! Recurring actions and updates.
// TODO `fetch` submodule for fetching new commits mod fetch;
// TODO `queue` submodule for updating the queue
mod queue; mod queue;
mod repo; mod repo;
use tracing::{debug_span, error, Instrument}; use tracing::{debug_span, error, warn_span, Instrument};
use super::{Repo, Server}; use super::{Repo, Server};
async fn recurring_task(state: &Server, repo: Repo) { async fn recurring_task(state: &Server, repo: Repo) {
fetch::update(state.config, repo.clone())
.instrument(debug_span!("fetch refs"))
.await;
async { async {
if let Err(e) = repo::update(&state.db, repo).await { if let Err(e) = repo::update(&state.db, repo).await {
error!("Error updating repo:\n{e:?}"); error!("Error updating repo:\n{e:?}");
@ -30,7 +32,10 @@ async fn recurring_task(state: &Server, repo: Repo) {
pub(super) async fn run(server: Server, repo: Repo) { pub(super) async fn run(server: Server, repo: Repo) {
loop { loop {
recurring_task(&server, repo.clone()).await; recurring_task(&server, repo.clone())
.instrument(warn_span!("update"))
.await;
tokio::time::sleep(server.config.repo_update).await; tokio::time::sleep(server.config.repo_update).await;
} }
} }

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:?}");
}
}
}