From d8e6a6cee9fb4f3952bbb8c05315ce634b20ba0a Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Tue, 21 May 2019 16:40:15 +0200 Subject: [PATCH] Force ssh clone, support github repo --- src/gclone.rs | 4 +++- src/git.rs | 23 ++++++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/gclone.rs b/src/gclone.rs index ed12d07..f588b84 100644 --- a/src/gclone.rs +++ b/src/gclone.rs @@ -4,7 +4,7 @@ extern crate log; use std::path::PathBuf; use std::process::exit; -use gclone::git::{clone, parse_url, GCLONE_PATH}; +use gclone::git::{clone, parse_url, parse_to_url, GCLONE_PATH}; use gclone::{first_arg, Cache, Result}; fn help() { @@ -37,6 +37,8 @@ fn main_result() -> Result<()> { path.push(&owner); path.push(&repo); + let url = parse_to_url(&server, &owner, &repo); + // Clone repository info!("{} {}{}", "cloning", url, "..."); clone(&url, &path)?; diff --git a/src/git.rs b/src/git.rs index cd43cf9..1a4c00e 100644 --- a/src/git.rs +++ b/src/git.rs @@ -38,8 +38,10 @@ macro_rules! unwrap { pub fn parse_url(input: &str) -> Result<(String, String, String)> { if input.starts_with("http") { parse_http_url(input) - } else { + } else if input.starts_with("ssh") { parse_ssh_url(input) + } else { + parse_github_url(input) } } @@ -71,6 +73,25 @@ fn parse_ssh_url(input: &str) -> Result<(String, String, String)> { Ok((server, owner, repo)) } +/// Parses a github url in the format owner/repo. +fn parse_github_url(input: &str) -> Result<(String, String, String)> { + let split = input.split("/").collect::>(); + if split.len() != 2 { + return Err(Error::GitUrlParseError); + } + + let server = String::from("github.com"); + let owner = String::from(split[0]); + let repo = String::from(split[1]); + + Ok((server, owner, repo)) +} + +/// Converts the parse result of a repo into an url. +pub fn parse_to_url(server: &str, owner: &str, repo: &str) -> String { + format!("git@{}:{}/{}", server, owner, repo) +} + /// Clones a git repository in the right place. /// /// If an error happens, it deletes the directory created.