use anyhow::Result; use git2::Repository; use std::path::PathBuf; pub(crate) fn has_tag(repo_path: PathBuf, tag: String) -> Result { let repo = Repository::init(repo_path)?; let tags = repo.tag_names(Some(&tag))?; for tag_obj in tags.iter() { if tag_obj.is_none() { continue; } let tag_name = tag_obj.unwrap(); if tag == tag_name.to_string() { return Ok(true); } } Ok(false) } #[cfg(test)] mod tests { use anyhow::Result; use git2::*; use std::{fs::File, io::Write, path::Path}; use tempfile::tempdir; #[test] fn has_tag() -> Result<()> { const TAG: &'static str = "0.1.0"; let dir = tempdir()?; let repo = Repository::init(&dir)?; let mut fout = File::create(&dir.path().join("VERSION"))?; write!(fout, "{}", TAG)?; drop(fout); let mut index = repo.index()?; index.add_path(Path::new("VERSION"))?; let oid = index.write_tree()?; let tree = repo.find_tree(oid)?; let sig = Signature::now("Testificate", "test@ifica.te")?; repo.commit( Some("HEAD"), &sig, &sig, "test commit please ignore", &tree, &[], )?; let obj = repo.revparse_single("HEAD")?; repo.tag(TAG, &obj, &sig, &format!("version {}", TAG), false)?; assert!(super::has_tag(dir.path().into(), TAG.into())?); Ok(()) } }