-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: github clone with ssh * fix: ssh keys can not be in HOME * chore: use git2_credentials for ssh auth * chore: clean println * refactor: remove duplicate git init * refactor: messages * fix: handle error. replace unwrap() * refactor: use prefix for urls * fix: ssh clone for up command * fix: test * nitpicks and lockfile update * Revert "nitpicks and lockfile update" This reverts commit bec9c0b. * refactor: bubble git2::config open error to caller --------- Co-authored-by: Abhishek Shah <[email protected]>
- Loading branch information
Showing
5 changed files
with
93 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,9 +1,12 @@ | ||
use crate::errors::Error; | ||
use anyhow::Result; | ||
use git2::{build::RepoBuilder, FetchOptions, IndexAddOption, Repository, ResetType}; | ||
use git2::{ | ||
build::RepoBuilder, FetchOptions, IndexAddOption, RemoteCallbacks, Repository, ResetType, | ||
}; | ||
use git2_credentials::CredentialHandler; | ||
use regex::Regex; | ||
use std::fs; | ||
use std::path::Path; | ||
use std::{env, fs}; | ||
use url::Url; | ||
|
||
pub struct Git; | ||
|
@@ -17,14 +20,36 @@ impl Git { | |
if let Some(branch) = branch { | ||
repo.branch(branch); | ||
} | ||
repo.clone(url.as_str(), working_dir)?; | ||
if let Err(_e) = repo.clone(url.as_str(), working_dir) { | ||
Self::ssh_clone(url, working_dir, branch)?; | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
pub(crate) fn ssh_clone(url: &Url, working_dir: &Path, branch: Option<&str>) -> Result<()> { | ||
// Change the url to the ssh url with [email protected]: prefix, remove / from path and adding .git as suffix | ||
let ssh_url = ["[email protected]:", &url.path()[1..], ".git"].concat(); | ||
if !working_dir.exists() { | ||
// Prepare callback and fetch options. | ||
let mut fo = FetchOptions::new(); | ||
Self::set_up_ssh_fetch_options(&mut fo)?; | ||
// Prepare builder and clone. | ||
let mut repo = RepoBuilder::new(); | ||
repo.fetch_options(fo); | ||
if let Some(branch) = branch { | ||
repo.branch(branch); | ||
} | ||
repo.clone(&ssh_url, working_dir)?; | ||
} | ||
Ok(()) | ||
} | ||
/// Clone `url` into `target` and degit it | ||
pub fn clone_and_degit(url: &str, target: &Path) -> Result<Option<String>> { | ||
let repo = Repository::clone(url, target)?; | ||
|
||
let repo = match Repository::clone(&["https://github.com/", url].concat(), target) { | ||
Ok(repo) => repo, | ||
Err(_e) => Self::ssh_clone_and_degit(url, target)?, | ||
}; | ||
// fetch tags from remote | ||
let release = Self::fetch_latest_tag(&repo); | ||
|
||
|
@@ -33,6 +58,31 @@ impl Git { | |
Ok(release) | ||
} | ||
|
||
/// For users that have ssh configuration for cloning repositories | ||
fn ssh_clone_and_degit(url: &str, target: &Path) -> Result<Repository> { | ||
// Prepare callback and fetch options. | ||
let mut fo = FetchOptions::new(); | ||
Self::set_up_ssh_fetch_options(&mut fo)?; | ||
// Prepare builder and clone. | ||
let mut builder = RepoBuilder::new(); | ||
builder.fetch_options(fo); | ||
let repo = builder.clone(&["[email protected]:", url].concat(), target)?; | ||
Ok(repo) | ||
} | ||
|
||
fn set_up_ssh_fetch_options(fo: &mut FetchOptions) -> Result<()> { | ||
let mut callbacks = RemoteCallbacks::new(); | ||
let git_config = git2::Config::open_default() | ||
.map_err(|e| Error::Config(format!("Cannot open git configuration: {}", e)))?; | ||
let mut ch = CredentialHandler::new(git_config); | ||
callbacks.credentials(move |url, username, allowed| { | ||
ch.try_next_credential(url, username, allowed) | ||
}); | ||
|
||
fo.remote_callbacks(callbacks); | ||
Ok(()) | ||
} | ||
|
||
/// Fetch the latest release from a repository | ||
fn fetch_latest_tag(repo: &Repository) -> Option<String> { | ||
let version_reg = Regex::new(r"v\d+\.\d+\.\d+").expect("Valid regex"); | ||
|