more refactoring

This commit is contained in:
Cadey Ratio 2020-07-09 11:35:49 -04:00
parent bc608f7ddc
commit 2cc49b86db
2 changed files with 19 additions and 21 deletions

View File

@ -14,7 +14,7 @@ pub(crate) async fn run(
let sig = git2::Signature::now(&common.username, &common.email)?; let sig = git2::Signature::now(&common.username, &common.email)?;
if !git::has_tag(&repo, tag.clone())? { if !git::has_tag(&repo, tag.clone())? {
git::tag_version(&repo, tag.clone(), desc.clone(), &sig)?; git::tag_version(&repo, &sig, tag.clone(), desc.clone())?;
let _ = git::push(&repo, common.token.clone(), git::TAGS); let _ = git::push(&repo, common.token.clone(), git::TAGS);
} else } else
/* The tag already exists */ /* The tag already exists */

View File

@ -1,57 +1,55 @@
use anyhow::Result; use anyhow::Result;
use git2::{Repository, Signature}; use git2::{
Cred, CredentialType, Direction, FetchOptions, PushOptions, RemoteCallbacks, Repository,
Signature,
};
use std::path::Path; use std::path::Path;
pub const TAGS: &'static [&'static str] = &["refs/tags/*:refs/tags/*"]; pub const TAGS: &'static [&'static str] = &["refs/tags/*:refs/tags/*"];
pub fn pull(repo: &Repository, token: String, what: &[&str]) -> Result<()> { pub fn pull(repo: &Repository, token: String, what: &[&str]) -> Result<()> {
let mut remote = repo.find_remote("origin")?; let mut remote = repo.find_remote("origin")?;
let mut fo = git2::FetchOptions::new(); let mut fo = FetchOptions::new();
let mut callbacks = git2::RemoteCallbacks::new(); let mut callbacks = RemoteCallbacks::new();
callbacks.credentials(|_u, _username, allowed_types| { callbacks.credentials(|_u, _username, allowed_types| {
if allowed_types.contains(git2::CredentialType::SSH_KEY) { if allowed_types.contains(CredentialType::SSH_KEY) {
let user = "git"; let user = "git";
git2::Cred::ssh_key_from_agent(user) Cred::ssh_key_from_agent(user)
} else { } else {
git2::Cred::userpass_plaintext(&token, "") Cred::userpass_plaintext(&token, "")
} }
}); });
fo.remote_callbacks(callbacks); fo.remote_callbacks(callbacks);
remote.connect(git2::Direction::Fetch)?; remote.connect(Direction::Fetch)?;
remote.fetch(what, Some(&mut fo), None)?; remote.fetch(what, Some(&mut fo), None)?;
Ok(()) Ok(())
} }
pub fn push(repo: &Repository, token: String, what: &[&str]) -> Result<()> { pub fn push(repo: &Repository, token: String, what: &[&str]) -> Result<()> {
let mut remote = repo.find_remote("origin")?; let mut remote = repo.find_remote("origin")?;
let mut po = git2::PushOptions::new(); let mut po = PushOptions::new();
let mut callbacks = git2::RemoteCallbacks::new(); let mut callbacks = RemoteCallbacks::new();
callbacks.credentials(|_u, _username, allowed_types| { callbacks.credentials(|_u, _username, allowed_types| {
if allowed_types.contains(git2::CredentialType::SSH_KEY) { if allowed_types.contains(CredentialType::SSH_KEY) {
let user = "git"; let user = "git";
git2::Cred::ssh_key_from_agent(user) Cred::ssh_key_from_agent(user)
} else { } else {
git2::Cred::userpass_plaintext(&token, "") Cred::userpass_plaintext(&token, "")
} }
}); });
po.remote_callbacks(callbacks); po.remote_callbacks(callbacks);
remote.connect(git2::Direction::Push)?; remote.connect(Direction::Push)?;
remote.push(what, Some(&mut po))?; remote.push(what, Some(&mut po))?;
Ok(()) Ok(())
} }
pub fn commit( pub fn commit(repo: &Repository, sig: &Signature, msg: &str, files: &[&str]) -> Result<()> {
repo: &Repository,
sig: &git2::Signature,
msg: &str,
files: &[&str],
) -> Result<()> {
let mut index = repo.index()?; let mut index = repo.index()?;
for file in files { for file in files {
index.add_path(Path::new(file))?; index.add_path(Path::new(file))?;
@ -64,7 +62,7 @@ pub fn commit(
Ok(()) Ok(())
} }
pub fn tag_version(repo: &Repository, tag: String, desc: String, sig: &git2::Signature) -> Result<()> { pub fn tag_version(repo: &Repository, sig: &Signature, tag: String, desc: String) -> Result<()> {
let obj = repo.revparse_single("HEAD")?; let obj = repo.revparse_single("HEAD")?;
repo.tag(&tag, &obj, &sig, &desc, false)?; repo.tag(&tag, &obj, &sig, &desc, false)?;