From 72273036b3019a60ecc5933ee2890973e36c7eb7 Mon Sep 17 00:00:00 2001 From: Bryson Steck Date: Sun, 2 Mar 2025 18:33:33 -0700 Subject: [PATCH] git cloning works --- Cargo.lock | 32 ++++++++++---------- build | 3 +- src/config.rs | 12 ++++---- src/main.rs | 5 ++-- src/refractr.rs | 79 ++++++++++++++++++++++++++++++++++++++++++++++--- 5 files changed, 102 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 81f6dc6..cdd0db4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -71,9 +71,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.29" +version = "4.5.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8acebd8ad879283633b343856142139f2da2317c96b05b4dd6181c61e2480184" +checksum = "027bb0d98429ae334a8698531da7077bdf906419543a35a55c2cb1b66437d767" dependencies = [ "clap_builder", "clap_derive", @@ -81,9 +81,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.29" +version = "4.5.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6ba32cbda51c7e1dfd49acc1457ba1a7dec5b64fe360e828acb13ca8dc9c2f9" +checksum = "5589e0cba072e0f3d23791efac0fd8627b49c829c196a492e88168e6a669d863" dependencies = [ "anstream", "anstyle", @@ -334,9 +334,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.169" +version = "0.2.170" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a" +checksum = "875b3680cb2f8f71bdcf9a30f38d48282f5d3c95cbf9b3fa57269bb5d5c06828" [[package]] name = "libgit2-sys" @@ -386,9 +386,9 @@ checksum = "23fb14cb19457329c82206317a5663005a4d404783dc74f4252769b0d5f42856" [[package]] name = "log" -version = "0.4.25" +version = "0.4.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04cbf5b083de1c7e0222a7a51dbfdba1cbe1c6ab0b15e29fff3f6c077fd9cd9f" +checksum = "30bde2b3dc3671ae49d8e2e9f044c7c005836e7a023ee57cffa25ab82764bb9e" [[package]] name = "memchr" @@ -464,18 +464,18 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.217" +version = "1.0.218" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "02fc4265df13d6fa1d00ecff087228cc0a2b5f3c0e87e258d8b94a156e984c70" +checksum = "e8dfc9d19bdbf6d17e22319da49161d5d0108e4188e8b680aef6299eed22df60" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.217" +version = "1.0.218" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a9bf7cf98d04a2b28aead066b7496853d4779c9cc183c440dbac457641e19a0" +checksum = "f09503e191f4e797cb8aac08e9a4a4695c5edf6a2e70e376d961ddd5c969f82b" dependencies = [ "proc-macro2", "quote", @@ -583,9 +583,9 @@ dependencies = [ [[package]] name = "unicode-ident" -version = "1.0.16" +version = "1.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a210d160f08b701c8721ba1c726c11662f877ea6b7094007e1ca9a1041945034" +checksum = "00e2473a93778eb0bad35909dff6a10d28e63f792f16ed15e404fca9d5eeedbe" [[package]] name = "url" @@ -707,9 +707,9 @@ checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" [[package]] name = "winnow" -version = "0.7.2" +version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "59690dea168f2198d1a3b0cac23b8063efcd11012f10ae4698f284808c8ef603" +checksum = "0e7f4ea97f6f78012141bcdb6a216b2609f0979ada50b20ca5b52dde2eac2bb1" dependencies = [ "memchr", ] diff --git a/build b/build index 319bb8a..63a9371 100755 --- a/build +++ b/build @@ -2,9 +2,10 @@ # Create all the different builds for refractr version=$(cat Cargo.toml | grep -m1 version | awk -F' ' '{print $3}' | sed 's|"||g') +cargo update +cargo clean # docker builds -cargo clean docker build -t refractr:$version . docker tag refractr:$version refractr:latest diff --git a/src/config.rs b/src/config.rs index f4a79e9..992dac1 100644 --- a/src/config.rs +++ b/src/config.rs @@ -85,12 +85,12 @@ impl fmt::Display for ConfigFile { #[derive(PartialEq)] #[derive(Deserialize)] pub struct Config { - from: String, - to: Vec, - branches: Option>, - work_dir: Option, - git: Git, - schedule: Schedule + pub from: String, + pub to: Vec, + pub branches: Option>, + pub work_dir: Option, + pub git: Git, + pub schedule: Schedule } #[derive(PartialEq)] diff --git a/src/main.rs b/src/main.rs index 3a49f18..5bd1f1b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -45,6 +45,7 @@ fn main() -> std::io::Result<()> { common::verbose(refractr.verbose, 3, format!("Printing sample config")); let example = include_str!("example/config.toml"); println!("{}", example); + Ok(()) } else { // warn to avoid root/admin if refractr.unix { @@ -58,13 +59,13 @@ fn main() -> std::io::Result<()> { let cfgs = config::read_config(args.config, &refractr); if refractr.verbose >= 2 { // no need to loop over configs if verbose is not at the correct level - for i in cfgs { + for i in &cfgs { common::verbose(refractr.verbose, 2, format!("{}", i)); } } common::verbose(refractr.verbose, 1, format!("Config file(s) read successfully")); + refractr::start(refractr, cfgs) } - Ok(()) } diff --git a/src/refractr.rs b/src/refractr.rs index 1b2de11..28cec16 100644 --- a/src/refractr.rs +++ b/src/refractr.rs @@ -1,9 +1,80 @@ +use git2::{Branch, Repository}; -// fn set_up_work_dir(work_dir: String) -> PathBuf { -// if -// } +use crate::common::{self, Refractr}; +use crate::config::{ConfigFile, Config}; +use std::fs; +use std::env; +use std::path::{Path, PathBuf}; +use git2::{Branches, Error}; + +fn set_up_work_dir(work_dir: PathBuf) -> String { + if let Err(e) = fs::create_dir_all(&work_dir) { + panic!("refractr: could not create working directory: {}", work_dir.to_string_lossy()) + } + work_dir.to_string_lossy().to_string() +} + +fn fast_forward(repo_dir: &str, branches: &Option>) -> Result<(), Error> { + let repo = Repository::open(repo_dir)?; + let branch_list: Vec = match branches { + Some(repo_branches) => repo_branches.to_vec(), + None => { + let mut strings = Vec::new(); + let remote_branches = repo.branches(Some(git2::BranchType::Remote))?; + for branch in remote_branches { + if let Ok((b, _)) = branch { + if let Ok(Some(name)) = b.name() { + strings.push(name.to_string()); + } + } + } + strings + } + }; + + repo.find_remote("origin")?.fetch(&branch_list, None, None)?; + Ok(()) +} + +pub fn start(refractr: Refractr, cfgs: Vec) -> std::io::Result<()> { + common::verbose(refractr.verbose, 3, format!("Starting main refractr loop")); + for i in &cfgs { + // set up the working directory + common::verbose(refractr.verbose, 3, format!("Loaded config: {}", i.path)); + let path_str = set_up_work_dir(match &i.config.work_dir { + None => { + if cfg!(windows) { + PathBuf::from(format!("\"{}\\refractr\"", match env::var("TEMP") { + Ok(val) => val, + Err(_) => format!("This shouldn't happen!") + })) + } else { + PathBuf::from("/tmp/refractr") + } + }, + Some(path) => PathBuf::from(path) + }); + + common::verbose(refractr.verbose, 2, format!("Created working directory: {}", &path_str)); + let repo_name = match &i.config.from.split("/").last() { + Some(split) => split.to_string(), + None => panic!("refractr: failed to parse repository name") + }; + + // make initial clone + common::verbose(refractr.verbose, 1, format!("Cloning repository: {}", &i.config.from)); + let repo_dir = &format!("{}/{}", &path_str, repo_name); + match Repository::clone(&i.config.from, Path::new(&repo_dir)) { + Ok(repo) => (), + Err(e) => { + eprintln!("refractr: warning: found existing repo at {}, attempting to use", repo_dir); + if let Err(e) = fast_forward(repo_dir, &i.config.branches) { + eprintln!("failed"); + } + } + } + } -pub fn start() -> std::io::Result<()> { Ok(()) }