fix: post-update receives refs from argv

This commit is contained in:
əlemi 2024-02-15 04:45:19 +01:00
parent 950c0aff74
commit f93e4ec43c
Signed by: alemi
GPG key ID: A4895B84D311642C
2 changed files with 14 additions and 43 deletions

View file

@ -3,14 +3,15 @@ pub struct TciRepo {
pub name: String,
pub path: std::path::PathBuf,
pub cfg: git2::Config,
pub updates: Vec<RefUpdate>,
pub refs: Vec<String>,
}
impl TciRepo {
pub fn new(updates_raw: &str) -> crate::error::TciResult<Self> {
pub fn new() -> crate::error::TciResult<Self> {
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<RefUpdate> = 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, updates })
}
Ok(TciRepo { name, path, cfg, refs })
}
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<RefUpdate> {
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))
}
}

View file

@ -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),
}
}