Compare commits

...

5 Commits

Author SHA1 Message Date
joeac 3ee4cc6dc4 pull and (TODO) build and (TODO) push 2026-06-01 08:36:03 +01:00
joeac c8cad70d59 adds git2 2026-05-15 15:29:59 +01:00
joeac f849de20bb can configure repositories, hello world response 2026-05-15 15:27:33 +01:00
joeac 2068342144 add anyhow, serde, toml, dirs 2026-05-15 14:39:59 +01:00
joeac e433feddbe installs rocket, hello world endpoint 2026-05-15 14:16:27 +01:00
5 changed files with 2342 additions and 2 deletions
Generated
+2211
View File
File diff suppressed because it is too large Load Diff
+6
View File
@@ -4,3 +4,9 @@ version = "0.1.0"
edition = "2024"
[dependencies]
anyhow = "1.0.102"
dirs = "6.0.0"
git2 = "0.20.4"
rocket = "0.5.1"
serde = "1.0.228"
toml = "1.1.2"
+52
View File
@@ -0,0 +1,52 @@
use anyhow::{Error, Result};
use git2::BranchType;
use crate::config::RepoConfig;
pub fn pull_or_clone_and_build_and_push(repo: &RepoConfig) -> Result<(), BuildError> {
let git_repo = pull_or_clone(repo)?;
build(&git_repo)?;
push(&git_repo)?;
Ok(())
}
fn pull_or_clone(repo: &RepoConfig) -> Result<git2::Repository, BuildError> {
match git2::Repository::open(&repo.local_git_repo) {
Ok(git_repo) => {
pull(&git_repo).map_err(|err| BuildError::FailedToPull(err))?;
Ok(git_repo)
},
Err(open_err) => match git2::Repository::clone(&repo.remote_git_repo, &repo.local_git_repo) {
Ok(git_repo) => Ok(git_repo),
Err(clone_err) => Err(BuildError::FailedToOpenOrClone(open_err.into(), clone_err.into())),
}
}
}
fn build(git_repo: &git2::Repository) -> Result<(), BuildError> {
todo!()
}
fn push(git_repo: &git2::Repository) -> Result<(), BuildError> {
todo!()
}
fn pull(git_repo: &git2::Repository) -> Result<()> {
let mut origin = git_repo.find_remote("origin")?;
let (main_branch, main_branch_name) = git_repo.find_branch("main", BranchType::Local).map(|b| (b, "main"))
.or_else(|_| git_repo.find_branch("master", BranchType::Local).map(|b| (b, "master")))?;
let upstream_main_branch = main_branch.upstream()?;
git_repo.set_head(main_branch_name)?;
origin.fetch(&[main_branch_name], None, None)?;
let upstream_commit = git_repo.reference_to_annotated_commit(&upstream_main_branch.into_reference())?;
git_repo.merge(&[&upstream_commit], None, None)?;
Ok(())
}
pub enum BuildError {
FailedToOpenOrClone(Error, Error),
FailedToPull(Error),
}
+24
View File
@@ -0,0 +1,24 @@
use std::path::PathBuf;
use anyhow::Context;
use serde::Deserialize;
pub fn read_config(path: &PathBuf) -> anyhow::Result<Config> {
let toml = std::fs::read(path)
.with_context(|| format!("Failed to read config file at {:#?}", path))?;
let config = toml::from_slice(&toml)
.with_context(|| format!("Failed to parse config file at {:#?}", path))?;
Ok(config)
}
#[derive(Debug, Default, Deserialize)]
pub struct Config {
pub repositories: Vec<RepoConfig>,
}
#[derive(Debug, Deserialize)]
pub struct RepoConfig {
pub remote_git_repo: String,
pub local_git_repo: String,
pub remote_package_repo: String,
}
+49 -2
View File
@@ -1,3 +1,50 @@
fn main() {
println!("Hello, world!");
use std::path::PathBuf;
use dirs::config_dir;
#[macro_use] extern crate rocket;
mod build;
mod config;
use config::read_config;
use rocket::{State, http::Status};
use crate::{build::{BuildError, pull_or_clone_and_build_and_push}, config::Config};
const BIN_NAME: &str = "build-joeac";
#[launch]
fn rocket() -> _ {
let config_path: PathBuf = config_dir().unwrap()
.join(BIN_NAME).join(BIN_NAME).with_added_extension("toml");
let config = read_config(&config_path).inspect_err(|err|
eprintln!("Failed to read config. Error: {}\nProceeding with defaults", err))
.unwrap_or(Config::default());
rocket::build().manage(config).mount("/", routes![index])
}
#[get("/<remote_git_repo..>")]
fn index(remote_git_repo: PathBuf, config: &State<Config>) -> (Status, String) {
match remote_git_repo.to_str() {
None => (Status::BadRequest, format!("Failed to convert request path to unicode: {:?}", remote_git_repo)),
Some(remote_git_repo) => match config.repositories.iter().find(|repo| repo.remote_git_repo.eq(remote_git_repo)) {
None => (Status::NotFound, format!("Remote git repository not configured: {remote_git_repo}")),
Some(repo) => match pull_or_clone_and_build_and_push(repo) {
Err(BuildError::FailedToOpenOrClone(open_err, clone_err)) => (
Status::InternalServerError,
format!(
"Failed to open or clone git repository. Error opening: {:#?}\n\nError cloning: {:#?}",
open_err,
clone_err)
),
Err(BuildError::FailedToPull(err)) => (
Status::InternalServerError,
format!("Failed to pull git repository. Error: {:#?}", err),
),
Ok(()) => (Status::Ok, String::from("Pulled git repository, now building and pushing")),
}
}
}
}