From 3e448ec51de07fd60758e62fc2e76aedb8d676e0 Mon Sep 17 00:00:00 2001 From: wzslr321 Date: Sun, 13 Oct 2024 15:43:08 +0200 Subject: [PATCH 1/6] feat: remove custom authentication and move to github's one --- Cargo.lock | 294 +++++++++++++++++++++++++++++++++++++++++++++-- Cargo.toml | 4 +- src/cli.rs | 32 +++--- src/git.rs | 37 +++--- src/transform.rs | 2 + src/utils/mod.rs | 35 ++++-- 6 files changed, 343 insertions(+), 61 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c39b024..9feb9b9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,21 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "addr2line" +version = "0.24.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfbe277e56a376000877090da837660b4427aad530e3028d44e0bffe4f89a1c1" +dependencies = [ + "gimli", +] + +[[package]] +name = "adler2" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" + [[package]] name = "ahash" version = "0.8.11" @@ -16,6 +31,15 @@ dependencies = [ "zerocopy", ] +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + [[package]] name = "anstream" version = "0.6.15" @@ -71,6 +95,25 @@ version = "1.0.89" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "86fdf8605db99b54d3cd748a44c6d04df638eb5dafb219b135d0149bd0db01f6" +[[package]] +name = "argon2rs" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f67b0b6a86dae6e67ff4ca2b6201396074996379fba2b92ff649126f37cb392" +dependencies = [ + "blake2-rfc", + "scoped_threadpool", +] + +[[package]] +name = "arrayvec" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd9fd44efafa8690358b7408d253adf110036b88f55672a933f01d616ad9b1b9" +dependencies = [ + "nodrop", +] + [[package]] name = "async-trait" version = "0.1.81" @@ -79,7 +122,7 @@ checksum = "6e0c28dcc82d7c8ead5cb13beb15405b57b8546e93215673ff8ca0349a028107" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.74", ] [[package]] @@ -88,6 +131,21 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" +[[package]] +name = "backtrace" +version = "0.3.74" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d82cb332cdfaed17ae235a638438ac4d4839913cc2af585c3c6746e8f8bee1a" +dependencies = [ + "addr2line", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", + "windows-targets", +] + [[package]] name = "base64" version = "0.21.7" @@ -103,6 +161,16 @@ dependencies = [ "serde", ] +[[package]] +name = "blake2-rfc" +version = "0.2.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d6d530bdd2d52966a6d03b7a964add7ae1a288d25214066fd4b600f0f796400" +dependencies = [ + "arrayvec", + "constant_time_eq", +] + [[package]] name = "block-buffer" version = "0.10.4" @@ -166,7 +234,7 @@ dependencies = [ "heck", "proc-macro2", "quote", - "syn", + "syn 2.0.74", ] [[package]] @@ -221,6 +289,12 @@ dependencies = [ "tiny-keccak", ] +[[package]] +name = "constant_time_eq" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc" + [[package]] name = "convert_case" version = "0.6.0" @@ -265,6 +339,17 @@ dependencies = [ "crypto-common", ] +[[package]] +name = "dirs-2" +version = "3.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77a8ea7942b7e715ee77e0bcb03910628a08ac2669f4cc84e904e86dc5e347f4" +dependencies = [ + "libc", + "redox_users", + "winapi", +] + [[package]] name = "dlv-list" version = "0.5.2" @@ -280,6 +365,28 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" +[[package]] +name = "failure" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d32e9bd16cc02eae7db7ef620b392808b89f6a5e16bb3497d159c6b92a0f4f86" +dependencies = [ + "backtrace", + "failure_derive", +] + +[[package]] +name = "failure_derive" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa4da3c766cd7a0db8242e326e9e4e081edd567072893ed320008189715366a4" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", + "synstructure", +] + [[package]] name = "form_urlencoded" version = "1.2.1" @@ -289,6 +396,12 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "fuchsia-cprng" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" + [[package]] name = "generic-array" version = "0.14.7" @@ -310,6 +423,12 @@ dependencies = [ "wasi", ] +[[package]] +name = "gimli" +version = "0.31.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" + [[package]] name = "git2" version = "0.19.0" @@ -360,8 +479,10 @@ dependencies = [ "anyhow", "clap", "config", + "dirs-2", "git2", "lazy_static", + "regex", "rhai", "serde", "serde_derive", @@ -502,6 +623,21 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" +[[package]] +name = "miniz_oxide" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2d80299ef12ff69b16a84bb182e3b9df68b5a91574d3d4fa6e41b65deec4df1" +dependencies = [ + "adler2", +] + +[[package]] +name = "nodrop" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb" + [[package]] name = "nom" version = "7.1.3" @@ -531,6 +667,15 @@ dependencies = [ "autocfg", ] +[[package]] +name = "object" +version = "0.36.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aedf0a2d09c573ed1d8d85b30c119153926a2b36dce0ab28322c09a117a4683e" +dependencies = [ + "memchr", +] + [[package]] name = "once_cell" version = "1.19.0" @@ -614,7 +759,7 @@ dependencies = [ "pest_meta", "proc-macro2", "quote", - "syn", + "syn 2.0.74", ] [[package]] @@ -667,6 +812,19 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293" +dependencies = [ + "fuchsia-cprng", + "libc", + "rand_core 0.3.1", + "rdrand", + "winapi", +] + [[package]] name = "rand" version = "0.8.5" @@ -675,7 +833,7 @@ checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" dependencies = [ "libc", "rand_chacha", - "rand_core", + "rand_core 0.6.4", ] [[package]] @@ -685,9 +843,24 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" dependencies = [ "ppv-lite86", - "rand_core", + "rand_core 0.6.4", +] + +[[package]] +name = "rand_core" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b" +dependencies = [ + "rand_core 0.4.2", ] +[[package]] +name = "rand_core" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc" + [[package]] name = "rand_core" version = "0.6.4" @@ -697,6 +870,62 @@ dependencies = [ "getrandom", ] +[[package]] +name = "rdrand" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" +dependencies = [ + "rand_core 0.3.1", +] + +[[package]] +name = "redox_syscall" +version = "0.1.57" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce" + +[[package]] +name = "redox_users" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "214a97e49be64fd2c86f568dd0cb2c757d2cc53de95b273b6ad0a1c908482f26" +dependencies = [ + "argon2rs", + "failure", + "rand 0.4.6", + "redox_syscall", +] + +[[package]] +name = "regex" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38200e5ee88914975b69f657f0801b6f6dccafd44fd9326302a4aaeecfacb1d8" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "368758f23274712b504848e9d5a6f010445cc8b87a7cdb4d7cbee666c1288da3" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" + [[package]] name = "rhai" version = "1.19.0" @@ -722,7 +951,7 @@ checksum = "a5a11a05ee1ce44058fa3d5961d05194fdbe3ad6b40f904af764d81b86450e6b" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.74", ] [[package]] @@ -747,12 +976,24 @@ dependencies = [ "ordered-multimap", ] +[[package]] +name = "rustc-demangle" +version = "0.1.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" + [[package]] name = "ryu" version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" +[[package]] +name = "scoped_threadpool" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d51f5df5af43ab3f1360b429fa5e0152ac5ce8c0bd6485cae490332e96846a8" + [[package]] name = "serde" version = "1.0.208" @@ -770,7 +1011,7 @@ checksum = "24008e81ff7613ed8e5ba0cfaf24e2c2f1e5b8a0495711e44fcd4882fca62bcf" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.74", ] [[package]] @@ -862,6 +1103,17 @@ version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "syn" version = "2.0.74" @@ -873,6 +1125,18 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "synstructure" +version = "0.12.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f36bdaa60a83aca3921b5259d5400cbf5e90fc51931376a9bd4a0eb79aa7210f" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", + "unicode-xid", +] + [[package]] name = "thin-vec" version = "0.2.13" @@ -896,7 +1160,7 @@ checksum = "08904e7672f5eb876eaaf87e0ce17857500934f4981c4a0ab2b4aa98baac7fc3" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.74", ] [[package]] @@ -986,7 +1250,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.74", ] [[package]] @@ -1063,6 +1327,12 @@ version = "1.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d4c87d22b6e3f4a18d4d40ef354e97c90fcb14dd91d7dc0aa9d8a1172ebf7202" +[[package]] +name = "unicode-xid" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" + [[package]] name = "unsafe-libyaml" version = "0.2.11" @@ -1093,7 +1363,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "81dfa00651efa65069b0b6b651f4aaa31ba9e3c3ce0137aaad053604ee7e0314" dependencies = [ "getrandom", - "rand", + "rand 0.8.5", "uuid-macro-internal", ] @@ -1105,7 +1375,7 @@ checksum = "ee1cd046f83ea2c4e920d6ee9f7c3537ef928d75dce5d84a87c2c5d6b3999a3a" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.74", ] [[package]] @@ -1263,5 +1533,5 @@ checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.74", ] diff --git a/Cargo.toml b/Cargo.toml index a2845e1..9ff71f8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,10 +7,12 @@ edition = "2021" [dependencies] anyhow = "1.0.89" -clap = { version = "4.5.16", features = ["derive"] } +clap = { version = "4.5.16", features = ["derive", "env"] } config = "0.14.0" +dirs-2 = "3.0.1" git2 = "0.19.0" lazy_static = "1.5.0" +regex = "1.11.0" rhai = "1.19.0" serde = "1.0.208" serde_derive = "1.0.208" diff --git a/src/cli.rs b/src/cli.rs index 8713831..c07c072 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -4,7 +4,7 @@ use std::io::Write; use std::path::Path; use clap::Parser; -use git2::Repository; +use git2::{Cred, CredentialType, Repository}; use serde_json::to_string_pretty; use thiserror::Error; use tracing::{error, info, trace, Level}; @@ -75,14 +75,12 @@ struct Args { #[arg(long, default_value_t=String::from("origin"))] origin: String, -} -// TODO: add more credentials variants -pub enum Credentials<'a> { - UsernamePassword { - username: &'a str, - password: &'a str, - }, + #[arg(long, env = "GIT_SSH_KEY")] + ssh_key_path: Option, + + #[clap(long, env = "GIT_PAT", help = "HTTPS Personal Access Token")] + https_pat: Option, } pub fn run() -> Result<(), CliError> { @@ -101,16 +99,15 @@ pub fn run() -> Result<(), CliError> { init_registry(cfg.custom_transform_scripts()); trace!("Transform functions initialized successfully"); - // TODO: Retrieve properly from args - let mock_credentials = utils::get_mock_credentials(); - let clone_into = match cfg.options.clone_into.as_deref() { Some(path) => path, None => Path::new("cloned_repository"), }; + let credentials = utils::get_ssh_credentials(args.ssh_key_path.unwrap()); + let repository_retrieval_result = match cfg.repository.url { - Some(url) => try_retrieve_repo_from_url(mock_credentials, &url, clone_into), + Some(url) => try_retrieve_repo_from_url(&credentials, &url, clone_into), None => match &cfg.repository.path { Some(path) => try_retrieve_repo_from_path(path), None => { @@ -127,7 +124,7 @@ pub fn run() -> Result<(), CliError> { }; trace!("Successfully retrieved repository"); - if let Err(fetch_err) = git::fetch_remote(&repository, &args.origin, &mock_credentials) { + if let Err(fetch_err) = git::fetch_remote(&repository, &args.origin, &credentials) { error!("Failed to fetch remote"); return Err(CliError::Unknown { err: Some(fetch_err), @@ -178,11 +175,14 @@ fn try_retrieve_repo_from_path(path: &Path) -> Result { } } -fn try_retrieve_repo_from_url( - credentials: &Credentials, +fn try_retrieve_repo_from_url<'a, F>( + credentials: F, url: &Url, clone_into: &Path, -) -> Result { +) -> Result +where + F: Fn(&str, Option<&str>, CredentialType) -> Result + 'a, +{ trace!("attempt to start from url-specified repository"); utils::prepare_directory(&clone_into) .with_context(|| "Failed to prepare directory for cloning")?; diff --git a/src/git.rs b/src/git.rs index 8148823..1ee1f75 100644 --- a/src/git.rs +++ b/src/git.rs @@ -3,13 +3,11 @@ use serde::Serialize; use std::path::Path; use thiserror::Error; -use git2::{Cred, RemoteCallbacks, Repository}; +use git2::{Cred, CredentialType, RemoteCallbacks, Repository}; use std::str; use tracing::{error, info, trace}; use url::Url; -use crate::cli::Credentials; - #[derive(Error, Debug)] pub enum GitError { #[error("Failed to authorize git request, due to authentication failure. Error:{err}")] @@ -55,11 +53,14 @@ pub fn extract_difference(repo: &Repository, options: &DiffOptions) -> Result Result<()> { +pub fn fetch_remote<'a, F>(repo: &Repository, remote_name: &str, credentials: F) -> Result<()> +where + F: Fn(&str, Option<&str>, CredentialType) -> Result + 'a, +{ let mut remote = repo.find_remote(remote_name)?; let mut callback = RemoteCallbacks::new(); - callback.credentials(|_url, _username_from_url, _allowed_types| credentials.into()); + callback.credentials(credentials); let mut fetch_options = git2::FetchOptions::new(); fetch_options.remote_callbacks(callback); @@ -120,30 +121,18 @@ pub fn open_repo(path: &Path) -> Result { } } -impl Credentials<'_> { - fn into(&self) -> Result { - let credentials = match self { - Credentials::UsernamePassword { username, password } => { - Cred::userpass_plaintext(&username, &password) - } - }; - - match credentials { - Ok(credentials) => Ok(credentials), - Err(err) => Err(err), - } - } -} - -pub fn clone_repo( - credentials: &Credentials, +pub fn clone_repo<'a, F>( + credentials: F, url: &Url, clone_into: &Path, -) -> Result { +) -> Result +where + F: Fn(&str, Option<&str>, CredentialType) -> Result + 'a, +{ info!("start cloning repository"); let mut callbacks = RemoteCallbacks::new(); - callbacks.credentials(|_url, _username_from_url, _allowed_types| credentials.into()); + callbacks.credentials(credentials); trace!("Callback credentials set to userpass_plaintext"); let mut builder = git2::build::RepoBuilder::new(); diff --git a/src/transform.rs b/src/transform.rs index 6467ee4..2fe9cf6 100644 --- a/src/transform.rs +++ b/src/transform.rs @@ -1,3 +1,4 @@ + use crate::config::CustomStep; use anyhow::Result; use rhai::{Dynamic, Engine, Map, Scope}; @@ -15,6 +16,7 @@ pub struct Context { pub class_name: Option, } +#[allow(dead_code)] pub trait TransformFn { fn execute( &self, diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 36c25f0..f46981f 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,8 +1,10 @@ -use std::{fs, path::Path}; -use tracing::{info, trace}; use anyhow::Result; - -use crate::cli::Credentials; +use git2::{Cred, CredentialType}; +use std::{ + fs, + path::Path, +}; +use tracing::{info, trace}; pub fn prepare_directory(path: &Path) -> Result<()> { if path.exists() { @@ -19,9 +21,26 @@ pub fn prepare_directory(path: &Path) -> Result<()> { Ok(()) } -pub fn get_mock_credentials<'a>() -> &'a Credentials<'a> { - &Credentials::UsernamePassword { - username: "wzslr321", - password: "TEST", +pub fn get_ssh_credentials( + ssh_key_path: String, +) -> impl Fn(&str, Option<&str>, CredentialType) -> Result { + // let key_path_owned: String = ssh_key_path + // .unwrap_or_else(|| { + // dirs_2::home_dir() + // .map(|p| p.join(".ssh").join("id_rsa").to_string_lossy().into_owned()) + // .unwrap_or_else(|| String::from("~/.ssh/id_rsa")) + // }); + + move |_url, username, allowed_types| { + if allowed_types.contains(CredentialType::SSH_KEY) { + Ok(Cred::ssh_key( + username.unwrap_or_else(|| "git"), + None, + Path::new(&ssh_key_path), + None, + )?) + } else { + Err(git2::Error::from_str("Unsupported credential type for SSH")) + } } } From b9b2f367fb25d124f1396c5913f51f450ca9e6ae Mon Sep 17 00:00:00 2001 From: wzslr321 Date: Sun, 13 Oct 2024 16:55:34 +0200 Subject: [PATCH 2/6] feat: remove unnecessary functions; ssh-key creds working --- src/cli.rs | 44 ++++++++++++++------------------------------ src/git.rs | 30 +++++++++++++++++++++++------- src/utils/mod.rs | 12 ++++++------ 3 files changed, 43 insertions(+), 43 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index c07c072..ce0af60 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -4,11 +4,10 @@ use std::io::Write; use std::path::Path; use clap::Parser; -use git2::{Cred, CredentialType, Repository}; +use git2::Repository; use serde_json::to_string_pretty; use thiserror::Error; use tracing::{error, info, trace, Level}; -use url::Url; use crate::config::Config; use crate::git; @@ -104,10 +103,21 @@ pub fn run() -> Result<(), CliError> { None => Path::new("cloned_repository"), }; - let credentials = utils::get_ssh_credentials(args.ssh_key_path.unwrap()); + let credentials = match args.ssh_key_path { + Some(path) => Some(utils::get_ssh_credentials(path)), + None => { + info!("No git credentials detected"); + None + } + }; let repository_retrieval_result = match cfg.repository.url { - Some(url) => try_retrieve_repo_from_url(&credentials, &url, clone_into), + Some(url) => { + if let Err(err) = utils::prepare_directory(clone_into) { + return Err(CliError::Unknown { err: Some(err) }); + } + git::clone_repo(&credentials, &url, clone_into).map_err(|err| anyhow!(err)) + } None => match &cfg.repository.path { Some(path) => try_retrieve_repo_from_path(path), None => { @@ -175,32 +185,6 @@ fn try_retrieve_repo_from_path(path: &Path) -> Result { } } -fn try_retrieve_repo_from_url<'a, F>( - credentials: F, - url: &Url, - clone_into: &Path, -) -> Result -where - F: Fn(&str, Option<&str>, CredentialType) -> Result + 'a, -{ - trace!("attempt to start from url-specified repository"); - utils::prepare_directory(&clone_into) - .with_context(|| "Failed to prepare directory for cloning")?; - - trace!("Starting to clone repository"); - let cloned_repo = match git::clone_repo(&credentials, url, &clone_into) { - Ok(repo) => repo, - Err(err) => { - return Err(anyhow!( - "Failed to clone repository from url.\nError: {}", - err - )); - } - }; - - Ok(cloned_repo) -} - fn setup_logging(tracing_level: u8) { let tracing_level = match tracing_level { 0 => Level::TRACE, diff --git a/src/git.rs b/src/git.rs index 1ee1f75..b4873c1 100644 --- a/src/git.rs +++ b/src/git.rs @@ -53,14 +53,21 @@ pub fn extract_difference(repo: &Repository, options: &DiffOptions) -> Result(repo: &Repository, remote_name: &str, credentials: F) -> Result<()> +pub fn fetch_remote<'a, F>( + repo: &Repository, + remote_name: &str, + credentials: &Option, +) -> Result<()> where F: Fn(&str, Option<&str>, CredentialType) -> Result + 'a, { let mut remote = repo.find_remote(remote_name)?; let mut callback = RemoteCallbacks::new(); - callback.credentials(credentials); + match credentials { + Some(credentials) => callback.credentials(credentials), + None => callback.credentials(|_url, _username, _allowed_types| git2::Cred::default()), + }; let mut fetch_options = git2::FetchOptions::new(); fetch_options.remote_callbacks(callback); @@ -78,7 +85,7 @@ pub fn extract_difference_branches( to_branch: &str, ) -> Result { // TODO: Those refs values most likely should not be hardcoded - let ref_from = repo.find_reference(&format!("refs/heads/{}", from_branch))?; + let ref_from = repo.find_reference(&format!("refs/remotes/origin/{}", from_branch))?; let ref_to = repo.find_reference(&format!("refs/remotes/origin/{}", to_branch))?; let commit_a = ref_from.peel_to_commit()?; @@ -122,18 +129,27 @@ pub fn open_repo(path: &Path) -> Result { } pub fn clone_repo<'a, F>( - credentials: F, + credentials: &Option, url: &Url, clone_into: &Path, -) -> Result +) -> Result where F: Fn(&str, Option<&str>, CredentialType) -> Result + 'a, { info!("start cloning repository"); let mut callbacks = RemoteCallbacks::new(); - callbacks.credentials(credentials); - trace!("Callback credentials set to userpass_plaintext"); + + match credentials { + Some(credentials) => { + trace!("Set git credentails to user specified ones"); + callbacks.credentials(credentials) + } + None => { + trace!("Set authenticationc allback to default git credentials"); + callbacks.credentials(|_url, _username_from_url, _allowed_types| git2::Cred::default()) + } + }; let mut builder = git2::build::RepoBuilder::new(); diff --git a/src/utils/mod.rs b/src/utils/mod.rs index f46981f..7b56f35 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,12 +1,10 @@ use anyhow::Result; use git2::{Cred, CredentialType}; -use std::{ - fs, - path::Path, -}; +use std::{fs, path::Path}; use tracing::{info, trace}; pub fn prepare_directory(path: &Path) -> Result<()> { + trace!("Preparing directory for repository cloning"); if path.exists() { if path.read_dir()?.next().is_some() { info!("Directory is not empty, removing existing files..."); @@ -33,12 +31,14 @@ pub fn get_ssh_credentials( move |_url, username, allowed_types| { if allowed_types.contains(CredentialType::SSH_KEY) { - Ok(Cred::ssh_key( + let cred = Cred::ssh_key( username.unwrap_or_else(|| "git"), None, Path::new(&ssh_key_path), None, - )?) + )?; + + Ok(cred) } else { Err(git2::Error::from_str("Unsupported credential type for SSH")) } From 59d6a83b2c2a09ad838e3eed85e22913c7a279e1 Mon Sep 17 00:00:00 2001 From: wzslr321 Date: Sun, 13 Oct 2024 17:44:27 +0200 Subject: [PATCH 3/6] feat: git pat auth --- src/cli.rs | 11 ++++------- src/git.rs | 1 + src/utils/mod.rs | 51 ++++++++++++++++++++++++++---------------------- 3 files changed, 33 insertions(+), 30 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index ce0af60..c672c33 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -80,6 +80,9 @@ struct Args { #[clap(long, env = "GIT_PAT", help = "HTTPS Personal Access Token")] https_pat: Option, + + #[arg(long, env="GIT_USERNAME", default_value_t=String::from("git"))] + username: String, } pub fn run() -> Result<(), CliError> { @@ -103,13 +106,7 @@ pub fn run() -> Result<(), CliError> { None => Path::new("cloned_repository"), }; - let credentials = match args.ssh_key_path { - Some(path) => Some(utils::get_ssh_credentials(path)), - None => { - info!("No git credentials detected"); - None - } - }; + let credentials = utils::get_git_credentials(args.ssh_key_path, args.username, args.https_pat); let repository_retrieval_result = match cfg.repository.url { Some(url) => { diff --git a/src/git.rs b/src/git.rs index b4873c1..54ba37a 100644 --- a/src/git.rs +++ b/src/git.rs @@ -69,6 +69,7 @@ where None => callback.credentials(|_url, _username, _allowed_types| git2::Cred::default()), }; + let mut fetch_options = git2::FetchOptions::new(); fetch_options.remote_callbacks(callback); diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 7b56f35..3461d60 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -19,28 +19,33 @@ pub fn prepare_directory(path: &Path) -> Result<()> { Ok(()) } -pub fn get_ssh_credentials( - ssh_key_path: String, -) -> impl Fn(&str, Option<&str>, CredentialType) -> Result { - // let key_path_owned: String = ssh_key_path - // .unwrap_or_else(|| { - // dirs_2::home_dir() - // .map(|p| p.join(".ssh").join("id_rsa").to_string_lossy().into_owned()) - // .unwrap_or_else(|| String::from("~/.ssh/id_rsa")) - // }); - - move |_url, username, allowed_types| { - if allowed_types.contains(CredentialType::SSH_KEY) { - let cred = Cred::ssh_key( - username.unwrap_or_else(|| "git"), - None, - Path::new(&ssh_key_path), - None, - )?; - - Ok(cred) - } else { - Err(git2::Error::from_str("Unsupported credential type for SSH")) - } +pub fn get_git_credentials( + ssh_key_path: Option, + username: String, + https_pat: Option, +) -> Option, CredentialType) -> Result> { + if let (None, None) = (&ssh_key_path, &https_pat) { + trace!("Neither ssh key path, nor https pat was specified"); + return None; } + + Some( + move |_url: &str, _username: Option<&str>, allowed_types: CredentialType| { + if let Some(ssh) = &ssh_key_path { + if allowed_types.contains(CredentialType::SSH_KEY) { + return Cred::ssh_key(&username, None, Path::new(&ssh), None); + } else { + return Err(git2::Error::from_str("Unsupported credential type for SSH")); + } + } else { + if allowed_types.contains(CredentialType::USER_PASS_PLAINTEXT) { + return Cred::userpass_plaintext(&username, &https_pat.clone().unwrap()); + } else { + return Err(git2::Error::from_str( + "Unsupported credential type for user_pass_plaintext", + )); + } + } + }, + ) } From 6db742d811cce885af5c0db00ed2a76d74cd4b92 Mon Sep 17 00:00:00 2001 From: wzslr321 Date: Tue, 15 Oct 2024 20:39:07 +0200 Subject: [PATCH 4/6] feat: update github workflow to not bloat PR --- .github/workflows/impactifier.yml | 2 +- src/cli.rs | 17 ++++++++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/.github/workflows/impactifier.yml b/.github/workflows/impactifier.yml index d7a5877..3742a64 100644 --- a/.github/workflows/impactifier.yml +++ b/.github/workflows/impactifier.yml @@ -51,7 +51,7 @@ jobs: env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | - ./target/release/impactifier --tracing-level=0 --from-branch=main --to-branch=refactor + ./target/release/impactifier --tracing-level=0 --from-branch=main --to-branch=main # 6. (Optional) Output diff.json for debugging - name: Output diff.json (Debug) diff --git a/src/cli.rs b/src/cli.rs index c672c33..d70fd27 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,4 +1,4 @@ -use anyhow::{anyhow, Context}; +use anyhow::anyhow; use std::fs::File; use std::io::Write; use std::path::Path; @@ -142,7 +142,7 @@ pub fn run() -> Result<(), CliError> { // TODO: Support other DiffOptions // // Current one is temporary, just for testing purposes - let diff = match git::extract_difference( + let _diff = match git::extract_difference( &repository, &git::DiffOptions::Branches { from: &args.from_branch.unwrap(), @@ -152,18 +152,25 @@ pub fn run() -> Result<(), CliError> { Ok(diff) => diff, Err(err) => { error!("Failed to extract difference"); + // Temporary, for testing purposes + save_run_result(false); return Err(CliError::Unknown { err: Some(err) }); } }; trace!("Successfuly extracted difference"); // Temporary, for testing purposes - let serialized_diff = to_string_pretty(&diff).unwrap(); + save_run_result(true); + + Ok(()) +} + +fn save_run_result(is_success: bool) { + let text = if is_success { "SUCCESS" } else { "FAILURE" }; + let serialized_diff = to_string_pretty(text).unwrap(); let mut file = File::create("./diff.json").unwrap(); file.write_all(serialized_diff.as_bytes()).unwrap(); - - Ok(()) } fn try_retrieve_repo_from_path(path: &Path) -> Result { From 5e436afbfeedc5b9efe68ecdd1a2d64b39120957 Mon Sep 17 00:00:00 2001 From: wzslr321 Date: Tue, 15 Oct 2024 20:40:35 +0200 Subject: [PATCH 5/6] chore: remove no longer used `dirs-2` dependency --- Cargo.lock | 255 +++-------------------------------------------------- Cargo.toml | 1 - 2 files changed, 12 insertions(+), 244 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9feb9b9..12aa303 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,21 +2,6 @@ # It is not intended for manual editing. version = 3 -[[package]] -name = "addr2line" -version = "0.24.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dfbe277e56a376000877090da837660b4427aad530e3028d44e0bffe4f89a1c1" -dependencies = [ - "gimli", -] - -[[package]] -name = "adler2" -version = "2.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" - [[package]] name = "ahash" version = "0.8.11" @@ -95,25 +80,6 @@ version = "1.0.89" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "86fdf8605db99b54d3cd748a44c6d04df638eb5dafb219b135d0149bd0db01f6" -[[package]] -name = "argon2rs" -version = "0.2.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f67b0b6a86dae6e67ff4ca2b6201396074996379fba2b92ff649126f37cb392" -dependencies = [ - "blake2-rfc", - "scoped_threadpool", -] - -[[package]] -name = "arrayvec" -version = "0.4.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cd9fd44efafa8690358b7408d253adf110036b88f55672a933f01d616ad9b1b9" -dependencies = [ - "nodrop", -] - [[package]] name = "async-trait" version = "0.1.81" @@ -122,7 +88,7 @@ checksum = "6e0c28dcc82d7c8ead5cb13beb15405b57b8546e93215673ff8ca0349a028107" dependencies = [ "proc-macro2", "quote", - "syn 2.0.74", + "syn", ] [[package]] @@ -131,21 +97,6 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" -[[package]] -name = "backtrace" -version = "0.3.74" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d82cb332cdfaed17ae235a638438ac4d4839913cc2af585c3c6746e8f8bee1a" -dependencies = [ - "addr2line", - "cfg-if", - "libc", - "miniz_oxide", - "object", - "rustc-demangle", - "windows-targets", -] - [[package]] name = "base64" version = "0.21.7" @@ -161,16 +112,6 @@ dependencies = [ "serde", ] -[[package]] -name = "blake2-rfc" -version = "0.2.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5d6d530bdd2d52966a6d03b7a964add7ae1a288d25214066fd4b600f0f796400" -dependencies = [ - "arrayvec", - "constant_time_eq", -] - [[package]] name = "block-buffer" version = "0.10.4" @@ -234,7 +175,7 @@ dependencies = [ "heck", "proc-macro2", "quote", - "syn 2.0.74", + "syn", ] [[package]] @@ -289,12 +230,6 @@ dependencies = [ "tiny-keccak", ] -[[package]] -name = "constant_time_eq" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc" - [[package]] name = "convert_case" version = "0.6.0" @@ -339,17 +274,6 @@ dependencies = [ "crypto-common", ] -[[package]] -name = "dirs-2" -version = "3.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77a8ea7942b7e715ee77e0bcb03910628a08ac2669f4cc84e904e86dc5e347f4" -dependencies = [ - "libc", - "redox_users", - "winapi", -] - [[package]] name = "dlv-list" version = "0.5.2" @@ -365,28 +289,6 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" -[[package]] -name = "failure" -version = "0.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d32e9bd16cc02eae7db7ef620b392808b89f6a5e16bb3497d159c6b92a0f4f86" -dependencies = [ - "backtrace", - "failure_derive", -] - -[[package]] -name = "failure_derive" -version = "0.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa4da3c766cd7a0db8242e326e9e4e081edd567072893ed320008189715366a4" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.109", - "synstructure", -] - [[package]] name = "form_urlencoded" version = "1.2.1" @@ -396,12 +298,6 @@ dependencies = [ "percent-encoding", ] -[[package]] -name = "fuchsia-cprng" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" - [[package]] name = "generic-array" version = "0.14.7" @@ -423,12 +319,6 @@ dependencies = [ "wasi", ] -[[package]] -name = "gimli" -version = "0.31.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" - [[package]] name = "git2" version = "0.19.0" @@ -479,7 +369,6 @@ dependencies = [ "anyhow", "clap", "config", - "dirs-2", "git2", "lazy_static", "regex", @@ -623,21 +512,6 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" -[[package]] -name = "miniz_oxide" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2d80299ef12ff69b16a84bb182e3b9df68b5a91574d3d4fa6e41b65deec4df1" -dependencies = [ - "adler2", -] - -[[package]] -name = "nodrop" -version = "0.1.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb" - [[package]] name = "nom" version = "7.1.3" @@ -667,15 +541,6 @@ dependencies = [ "autocfg", ] -[[package]] -name = "object" -version = "0.36.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aedf0a2d09c573ed1d8d85b30c119153926a2b36dce0ab28322c09a117a4683e" -dependencies = [ - "memchr", -] - [[package]] name = "once_cell" version = "1.19.0" @@ -759,7 +624,7 @@ dependencies = [ "pest_meta", "proc-macro2", "quote", - "syn 2.0.74", + "syn", ] [[package]] @@ -812,19 +677,6 @@ dependencies = [ "proc-macro2", ] -[[package]] -name = "rand" -version = "0.4.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293" -dependencies = [ - "fuchsia-cprng", - "libc", - "rand_core 0.3.1", - "rdrand", - "winapi", -] - [[package]] name = "rand" version = "0.8.5" @@ -833,7 +685,7 @@ checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" dependencies = [ "libc", "rand_chacha", - "rand_core 0.6.4", + "rand_core", ] [[package]] @@ -843,24 +695,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" dependencies = [ "ppv-lite86", - "rand_core 0.6.4", -] - -[[package]] -name = "rand_core" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b" -dependencies = [ - "rand_core 0.4.2", + "rand_core", ] -[[package]] -name = "rand_core" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc" - [[package]] name = "rand_core" version = "0.6.4" @@ -870,33 +707,6 @@ dependencies = [ "getrandom", ] -[[package]] -name = "rdrand" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" -dependencies = [ - "rand_core 0.3.1", -] - -[[package]] -name = "redox_syscall" -version = "0.1.57" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce" - -[[package]] -name = "redox_users" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "214a97e49be64fd2c86f568dd0cb2c757d2cc53de95b273b6ad0a1c908482f26" -dependencies = [ - "argon2rs", - "failure", - "rand 0.4.6", - "redox_syscall", -] - [[package]] name = "regex" version = "1.11.0" @@ -951,7 +761,7 @@ checksum = "a5a11a05ee1ce44058fa3d5961d05194fdbe3ad6b40f904af764d81b86450e6b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.74", + "syn", ] [[package]] @@ -976,24 +786,12 @@ dependencies = [ "ordered-multimap", ] -[[package]] -name = "rustc-demangle" -version = "0.1.24" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" - [[package]] name = "ryu" version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" -[[package]] -name = "scoped_threadpool" -version = "0.1.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d51f5df5af43ab3f1360b429fa5e0152ac5ce8c0bd6485cae490332e96846a8" - [[package]] name = "serde" version = "1.0.208" @@ -1011,7 +809,7 @@ checksum = "24008e81ff7613ed8e5ba0cfaf24e2c2f1e5b8a0495711e44fcd4882fca62bcf" dependencies = [ "proc-macro2", "quote", - "syn 2.0.74", + "syn", ] [[package]] @@ -1103,17 +901,6 @@ version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" -[[package]] -name = "syn" -version = "1.0.109" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - [[package]] name = "syn" version = "2.0.74" @@ -1125,18 +912,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "synstructure" -version = "0.12.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f36bdaa60a83aca3921b5259d5400cbf5e90fc51931376a9bd4a0eb79aa7210f" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.109", - "unicode-xid", -] - [[package]] name = "thin-vec" version = "0.2.13" @@ -1160,7 +935,7 @@ checksum = "08904e7672f5eb876eaaf87e0ce17857500934f4981c4a0ab2b4aa98baac7fc3" dependencies = [ "proc-macro2", "quote", - "syn 2.0.74", + "syn", ] [[package]] @@ -1250,7 +1025,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.74", + "syn", ] [[package]] @@ -1327,12 +1102,6 @@ version = "1.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d4c87d22b6e3f4a18d4d40ef354e97c90fcb14dd91d7dc0aa9d8a1172ebf7202" -[[package]] -name = "unicode-xid" -version = "0.2.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" - [[package]] name = "unsafe-libyaml" version = "0.2.11" @@ -1363,7 +1132,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "81dfa00651efa65069b0b6b651f4aaa31ba9e3c3ce0137aaad053604ee7e0314" dependencies = [ "getrandom", - "rand 0.8.5", + "rand", "uuid-macro-internal", ] @@ -1375,7 +1144,7 @@ checksum = "ee1cd046f83ea2c4e920d6ee9f7c3537ef928d75dce5d84a87c2c5d6b3999a3a" dependencies = [ "proc-macro2", "quote", - "syn 2.0.74", + "syn", ] [[package]] @@ -1533,5 +1302,5 @@ checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.74", + "syn", ] diff --git a/Cargo.toml b/Cargo.toml index 9ff71f8..ecfd2c9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,6 @@ edition = "2021" anyhow = "1.0.89" clap = { version = "4.5.16", features = ["derive", "env"] } config = "0.14.0" -dirs-2 = "3.0.1" git2 = "0.19.0" lazy_static = "1.5.0" regex = "1.11.0" From ff315c9cc103837f3268f10adafd77429999802d Mon Sep 17 00:00:00 2001 From: wzslr321 Date: Tue, 15 Oct 2024 21:13:37 +0200 Subject: [PATCH 6/6] feat: organize credentials passing & logging in its retrieval --- src/git.rs | 25 ++++------------------- src/utils/mod.rs | 52 ++++++++++++++++++++++++++++-------------------- 2 files changed, 34 insertions(+), 43 deletions(-) diff --git a/src/git.rs b/src/git.rs index 54ba37a..997c3f1 100644 --- a/src/git.rs +++ b/src/git.rs @@ -53,22 +53,14 @@ pub fn extract_difference(repo: &Repository, options: &DiffOptions) -> Result( - repo: &Repository, - remote_name: &str, - credentials: &Option, -) -> Result<()> +pub fn fetch_remote<'a, F>(repo: &Repository, remote_name: &str, credentials: F) -> Result<()> where F: Fn(&str, Option<&str>, CredentialType) -> Result + 'a, { let mut remote = repo.find_remote(remote_name)?; let mut callback = RemoteCallbacks::new(); - match credentials { - Some(credentials) => callback.credentials(credentials), - None => callback.credentials(|_url, _username, _allowed_types| git2::Cred::default()), - }; - + callback.credentials(credentials); let mut fetch_options = git2::FetchOptions::new(); fetch_options.remote_callbacks(callback); @@ -130,7 +122,7 @@ pub fn open_repo(path: &Path) -> Result { } pub fn clone_repo<'a, F>( - credentials: &Option, + credentials: F, url: &Url, clone_into: &Path, ) -> Result @@ -141,16 +133,7 @@ where let mut callbacks = RemoteCallbacks::new(); - match credentials { - Some(credentials) => { - trace!("Set git credentails to user specified ones"); - callbacks.credentials(credentials) - } - None => { - trace!("Set authenticationc allback to default git credentials"); - callbacks.credentials(|_url, _username_from_url, _allowed_types| git2::Cred::default()) - } - }; + callbacks.credentials(credentials); let mut builder = git2::build::RepoBuilder::new(); diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 3461d60..7bd8ef1 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -23,29 +23,37 @@ pub fn get_git_credentials( ssh_key_path: Option, username: String, https_pat: Option, -) -> Option, CredentialType) -> Result> { - if let (None, None) = (&ssh_key_path, &https_pat) { - trace!("Neither ssh key path, nor https pat was specified"); - return None; - } +) -> impl Fn(&str, Option<&str>, CredentialType) -> Result { + match (&ssh_key_path, &https_pat) { + (None, None) => + trace!("Neither ssh key path, nor https pat was specified. Fallback set to default git credentials"), + (None, Some(_)) => + trace!("HTTPS PAT was specified and will be used for git credentials creation along username: {}", username), + (Some(_), None) => + trace!("SSH Key was specified and will be used for git credentials"), + (Some(_), Some(_)) => + trace!("both SSH Key and HTTPS PAT were specified, but only SSH Key will be used for git credentials"), + }; + + move |_url: &str, _username: Option<&str>, allowed_types: CredentialType| { + if let (None, None) = (&ssh_key_path, &https_pat) { + return git2::Cred::default(); + } - Some( - move |_url: &str, _username: Option<&str>, allowed_types: CredentialType| { - if let Some(ssh) = &ssh_key_path { - if allowed_types.contains(CredentialType::SSH_KEY) { - return Cred::ssh_key(&username, None, Path::new(&ssh), None); - } else { - return Err(git2::Error::from_str("Unsupported credential type for SSH")); - } + if let Some(ssh) = &ssh_key_path { + if allowed_types.contains(CredentialType::SSH_KEY) { + Cred::ssh_key(&username, None, Path::new(&ssh), None) } else { - if allowed_types.contains(CredentialType::USER_PASS_PLAINTEXT) { - return Cred::userpass_plaintext(&username, &https_pat.clone().unwrap()); - } else { - return Err(git2::Error::from_str( - "Unsupported credential type for user_pass_plaintext", - )); - } + Err(git2::Error::from_str("Unsupported credential type for SSH")) } - }, - ) + } else { + if allowed_types.contains(CredentialType::USER_PASS_PLAINTEXT) { + Cred::userpass_plaintext(&username, &https_pat.clone().unwrap()) + } else { + Err(git2::Error::from_str( + "Unsupported credential type for user_pass_plaintext", + )) + } + } + } }