From 2cc49b86db7a35b1b8b26969bad2729b31ce7621 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Thu, 9 Jul 2020 11:35:49 -0400 Subject: [PATCH] more refactoring --- src/cmd/cut.rs | 2 +- src/git.rs | 38 ++++++++++++++++++-------------------- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/src/cmd/cut.rs b/src/cmd/cut.rs index 819dbcd..450f838 100644 --- a/src/cmd/cut.rs +++ b/src/cmd/cut.rs @@ -14,7 +14,7 @@ pub(crate) async fn run( let sig = git2::Signature::now(&common.username, &common.email)?; 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); } else /* The tag already exists */ diff --git a/src/git.rs b/src/git.rs index dabd4a3..2578d97 100644 --- a/src/git.rs +++ b/src/git.rs @@ -1,57 +1,55 @@ use anyhow::Result; -use git2::{Repository, Signature}; +use git2::{ + Cred, CredentialType, Direction, FetchOptions, PushOptions, RemoteCallbacks, Repository, + Signature, +}; use std::path::Path; pub const TAGS: &'static [&'static str] = &["refs/tags/*:refs/tags/*"]; pub fn pull(repo: &Repository, token: String, what: &[&str]) -> Result<()> { 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| { - if allowed_types.contains(git2::CredentialType::SSH_KEY) { + if allowed_types.contains(CredentialType::SSH_KEY) { let user = "git"; - git2::Cred::ssh_key_from_agent(user) + Cred::ssh_key_from_agent(user) } else { - git2::Cred::userpass_plaintext(&token, "") + Cred::userpass_plaintext(&token, "") } }); fo.remote_callbacks(callbacks); - remote.connect(git2::Direction::Fetch)?; + remote.connect(Direction::Fetch)?; remote.fetch(what, Some(&mut fo), None)?; Ok(()) } pub fn push(repo: &Repository, token: String, what: &[&str]) -> Result<()> { 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| { - if allowed_types.contains(git2::CredentialType::SSH_KEY) { + if allowed_types.contains(CredentialType::SSH_KEY) { let user = "git"; - git2::Cred::ssh_key_from_agent(user) + Cred::ssh_key_from_agent(user) } else { - git2::Cred::userpass_plaintext(&token, "") + Cred::userpass_plaintext(&token, "") } }); po.remote_callbacks(callbacks); - remote.connect(git2::Direction::Push)?; + remote.connect(Direction::Push)?; remote.push(what, Some(&mut po))?; Ok(()) } -pub fn commit( - repo: &Repository, - sig: &git2::Signature, - msg: &str, - files: &[&str], -) -> Result<()> { +pub fn commit(repo: &Repository, sig: &Signature, msg: &str, files: &[&str]) -> Result<()> { let mut index = repo.index()?; for file in files { index.add_path(Path::new(file))?; @@ -64,7 +62,7 @@ pub fn commit( 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")?; repo.tag(&tag, &obj, &sig, &desc, false)?;