diff options
Diffstat (limited to 'asyncgit/src/sync/cred.rs')
-rw-r--r-- | asyncgit/src/sync/cred.rs | 51 |
1 files changed, 50 insertions, 1 deletions
diff --git a/asyncgit/src/sync/cred.rs b/asyncgit/src/sync/cred.rs index 3f908a63..54eb2cce 100644 --- a/asyncgit/src/sync/cred.rs +++ b/asyncgit/src/sync/cred.rs @@ -1,7 +1,12 @@ //! credentials git helper use super::{ - remotes::get_default_remote_in_repo, repository::repo, RepoPath, + remotes::{ + get_default_remote_for_push_in_repo, + get_default_remote_in_repo, + }, + repository::repo, + RepoPath, }; use crate::error::{Error, Result}; use git2::CredentialHelper; @@ -43,6 +48,22 @@ pub fn need_username_password(repo_path: &RepoPath) -> Result<bool> { Ok(is_http) } +/// know if username and password are needed for this url +pub fn need_username_password_for_push( + repo_path: &RepoPath, +) -> Result<bool> { + let repo = repo(repo_path)?; + let remote = repo + .find_remote(&get_default_remote_for_push_in_repo(&repo)?)?; + let url = remote + .pushurl() + .or_else(|| remote.url()) + .ok_or(Error::UnknownRemote)? + .to_owned(); + let is_http = url.starts_with("http"); + Ok(is_http) +} + /// extract username and password pub fn extract_username_password( repo_path: &RepoPath, @@ -71,6 +92,34 @@ pub fn extract_username_password( }) } +/// extract username and password +pub fn extract_username_password_for_push( + repo_path: &RepoPath, +) -> Result<BasicAuthCredential> { + let repo = repo(repo_path)?; + let url = repo + .find_remote(&get_default_remote_for_push_in_repo(&repo)?)? + .url() + .ok_or(Error::UnknownRemote)? + .to_owned(); + let mut helper = CredentialHelper::new(&url); + + //TODO: look at Cred::credential_helper, + //if the username is in the url we need to set it here, + //I dont think `config` will pick it up + + if let Ok(config) = repo.config() { + helper.config(&config); + } + + Ok(match helper.execute() { + Some((username, password)) => { + BasicAuthCredential::new(Some(username), Some(password)) + } + None => extract_cred_from_url(&url), + }) +} + /// extract credentials from url pub fn extract_cred_from_url(url: &str) -> BasicAuthCredential { url::Url::parse(url).map_or_else( |