From f93e4ec43cc1e3d0f8da2321549f15fd65d27680 Mon Sep 17 00:00:00 2001 From: alemi Date: Thu, 15 Feb 2024 04:45:19 +0100 Subject: [PATCH] fix: post-update receives refs from argv --- src/git.rs | 38 +++++++++----------------------------- src/tci.rs | 19 +++++-------------- 2 files changed, 14 insertions(+), 43 deletions(-) diff --git a/src/git.rs b/src/git.rs index 15fadb1..a5633e7 100644 --- a/src/git.rs +++ b/src/git.rs @@ -3,14 +3,15 @@ pub struct TciRepo { pub name: String, pub path: std::path::PathBuf, pub cfg: git2::Config, - pub updates: Vec, + pub refs: Vec, } impl TciRepo { - pub fn new(updates_raw: &str) -> crate::error::TciResult { + pub fn new() -> crate::error::TciResult { let home = std::env::var("HOME") .unwrap_or_default(); + let path = match std::env::var("GIT_DIR") { Ok(p) => std::path::PathBuf::from(&p), Err(_) => std::env::current_dir()?, @@ -26,35 +27,14 @@ impl TciRepo { name.remove(name.chars().count()); } + let refs = std::env::args().skip(1).collect(); + let cfg = git2::Config::open(&path.join("config"))?; - let updates : Vec = updates_raw.split('\n') - .filter_map(|l| { - let split : Vec<&str> = l.split(' ').collect(); - Some(RefUpdate { - from: (*split.get(0)?).to_string(), - to: (*split.get(1)?).to_string(), - branch: (*split.get(2)?).to_string(), - }) - }) - .collect(); + Ok(TciRepo { name, path, cfg, refs }) + } - Ok(TciRepo { name, path, cfg, updates }) - } -} - -pub struct RefUpdate { - pub from: String, - pub to: String, - pub branch: String, -} - -pub trait RefUpdateVecHelper { - fn contains_branch(&self, branch: &str) -> bool; -} - -impl RefUpdateVecHelper for Vec { - fn contains_branch(&self, branch: &str) -> bool { - self.iter().any(|rf| rf.branch == branch) + pub fn updated(&self, search: &str) -> bool { + self.refs.iter().any(|r| r.ends_with(search)) } } diff --git a/src/tci.rs b/src/tci.rs index aed05e5..692d0b1 100644 --- a/src/tci.rs +++ b/src/tci.rs @@ -1,6 +1,4 @@ -use std::io::Read; - -use crate::{cfg::TciConfig, error::{TciErr, TciResult}, git::{RefUpdateVecHelper, TciRepo}}; +use crate::{cfg::TciConfig, error::{TciErr, TciResult}, git::TciRepo}; pub struct Tci { cfg: TciConfig, @@ -16,15 +14,7 @@ impl Tci { TciConfig::default() }); - // check which branches are being updated - let mut stdin = String::new(); - std::io::stdin().read_to_string(&mut stdin) - .unwrap_or_else(|e| { - eprintln!("[!] could not read refs from stdin: {e}"); - 0 - }); - - let repo = TciRepo::new(&stdin)?; + let repo = TciRepo::new()?; Ok(Tci{cfg, repo}) } @@ -35,10 +25,11 @@ impl Tci { return false; // we are in whitelist mode and this repo is not whitelisted } + match self.cfg.branch.as_deref() { - None => self.repo.updates.contains_branch("tci"), + None => self.repo.updated("tci"), Some("") => true, - Some(b) => self.repo.updates.contains_branch(b), + Some(b) => self.repo.updated(b), } }