-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
194 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use std::path::Path; | ||
|
||
use git2::{Cred, RemoteCallbacks, Repository}; | ||
|
||
use crate::config::GitConfig; | ||
|
||
/// Attempt to clone a remote repo from a [crate::config::GitConfig] | ||
pub fn from_clone(path: String, config: &GitConfig) -> Result<Repository, git2::Error> | ||
{ | ||
if let GitConfig{auth: Some(_), remote: _, branch: _} = config | ||
{ | ||
let auth = config.auth.clone().unwrap(); | ||
let result = match &auth.key_path | ||
{ | ||
Some(_) => | ||
{ | ||
let mut callbacks = RemoteCallbacks::new(); | ||
callbacks.credentials(|_url, _username_from_url, _allowed_types| | ||
{ | ||
Cred::ssh_key( | ||
&auth.user, | ||
None, | ||
Path::new(&auth.key_path.clone().unwrap()), | ||
Some(&auth.passphrase), | ||
) | ||
}); | ||
callbacks | ||
}, | ||
None => | ||
{ | ||
let mut callbacks = RemoteCallbacks::new(); | ||
callbacks.credentials(|_url, _username_from_url, _allowed_types| | ||
{ | ||
Cred::userpass_plaintext( | ||
&auth.user, | ||
&auth.passphrase, | ||
) | ||
}); | ||
callbacks | ||
} | ||
}; | ||
|
||
let mut fo = git2::FetchOptions::new(); | ||
fo.remote_callbacks(result); | ||
let mut builder = git2::build::RepoBuilder::new(); | ||
builder.fetch_options(fo); | ||
builder.branch(&config.branch); | ||
match builder.clone(&config.remote,Path::new(&path)) | ||
{ | ||
Ok(repo) => Ok(repo), | ||
Err(e) => | ||
{ | ||
crate::debug(format!("Error {} while cloning (authenticated) repo at {}", e, config.remote), None); | ||
Err(e) | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
match Repository::clone(&config.remote, path) | ||
{ | ||
Ok(repo) => Ok(repo), | ||
Err(e) => | ||
{ | ||
crate::debug(format!("Error {} while cloning (pub) repo at {}", e, config.remote), None); | ||
Err(e) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
mod common; | ||
|
||
#[cfg(test)] | ||
mod git | ||
{ | ||
use std::{fs::remove_dir_all, path::Path}; | ||
|
||
use busser::{config::{GitAuthConfig, GitConfig}, integrations::git::from_clone}; | ||
|
||
#[test] | ||
pub fn test_clone() | ||
{ | ||
let config = GitConfig | ||
{ | ||
remote: "https://github.com/JerboaBurrow/Busser".into(), | ||
branch: "main".into(), | ||
auth: None, | ||
}; | ||
|
||
let path = "tests/test_clone"; | ||
let repo = from_clone(path.into(), &config); | ||
|
||
assert!(repo.is_ok()); | ||
assert!(!repo.as_ref().unwrap().is_empty().unwrap()); | ||
assert!(Path::exists(Path::new(path))); | ||
|
||
if Path::exists(Path::new(path)) | ||
{ | ||
let _ = remove_dir_all(Path::new(path)); | ||
} | ||
} | ||
|
||
#[test] | ||
pub fn test_key_authed_clone() | ||
{ | ||
let config = GitConfig | ||
{ | ||
remote: "https://github.com/JerboaBurrow/test".into(), | ||
branch: "main".into(), | ||
auth: Some(GitAuthConfig | ||
{ | ||
key_path: Some("not_a_key".into()), | ||
user: "not_a_user".into(), | ||
passphrase: "not_a_passphrase".into(), | ||
}), | ||
}; | ||
|
||
let path = "tests/test_key_authed_clone"; | ||
let repo = from_clone(path.into(), &config); | ||
|
||
assert!(repo.is_err()); | ||
|
||
if Path::exists(Path::new(path)) | ||
{ | ||
let _ = remove_dir_all(Path::new(path)); | ||
} | ||
} | ||
|
||
#[test] | ||
pub fn test_pass_authed_clone() | ||
{ | ||
let config = GitConfig | ||
{ | ||
remote: "https://github.com/JerboaBurrow/test".into(), | ||
branch: "main".into(), | ||
auth: Some(GitAuthConfig | ||
{ | ||
key_path: None, | ||
user: "not_a_user".into(), | ||
passphrase: "not_a_passphrase".into(), | ||
}), | ||
}; | ||
|
||
let path = "tests/test_pass_authed_clone"; | ||
let repo = from_clone(path.into(), &config); | ||
|
||
assert!(repo.is_err()); | ||
|
||
if Path::exists(Path::new(path)) | ||
{ | ||
let _ = remove_dir_all(Path::new(path)); | ||
} | ||
} | ||
} |