cred: check pushurl before checking url (#967)

* cred: check pushurl before checking url

if a remote has both a pushurl and a url, need_username_password should check the pushurl before checking the url since we might not need credentials for the former
This commit is contained in:
Martin Kühl 2021-12-09 22:47:27 +01:00 committed by GitHub
parent 9eb30ecb4c
commit 5550415e3c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 3 deletions

View File

@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- Keep commit message when pre-commit hook fails ([#1035](https://github.com/extrawurst/gitui/issues/1035))
- honor `pushurl` when checking whether we need username and password for pushing ([#953](https://github.com/extrawurst/gitui/issues/953))
## [0.19] - 2021-12-08 - Bare Repo Support

View File

@ -32,9 +32,11 @@ impl BasicAuthCredential {
/// know if username and password are needed for this url
pub fn need_username_password(repo_path: &RepoPath) -> Result<bool> {
let repo = repo(repo_path)?;
let url = repo
.find_remote(&get_default_remote_in_repo(&repo)?)?
.url()
let remote =
repo.find_remote(&get_default_remote_in_repo(&repo)?)?;
let url = remote
.pushurl()
.or_else(|| remote.url())
.ok_or(Error::UnknownRemote)?
.to_owned();
let is_http = url.starts_with("http");
@ -188,6 +190,25 @@ mod tests {
assert_eq!(need_username_password(repo_path).unwrap(), false);
}
#[test]
#[serial]
fn test_dont_need_username_password_if_pushurl_ssh() {
let (_td, repo) = repo_init().unwrap();
let root = repo.path().parent().unwrap();
let repo_path: &RepoPath =
&root.as_os_str().to_str().unwrap().into();
repo.remote(DEFAULT_REMOTE_NAME, "http://user@github.com")
.unwrap();
repo.remote_set_pushurl(
DEFAULT_REMOTE_NAME,
Some("git@github.com:user/repo"),
)
.unwrap();
assert_eq!(need_username_password(repo_path).unwrap(), false);
}
#[test]
#[serial]
#[should_panic]