|
|
|
@ -4,7 +4,7 @@ mod git;
|
|
|
|
|
use std::borrow::Cow;
|
|
|
|
|
|
|
|
|
|
use error::{AppError, ErrorType};
|
|
|
|
|
use git::{Domain, GitOutput, GitTrait, Local, Url};
|
|
|
|
|
use git::{GitOutput, GitTrait, Local, Url};
|
|
|
|
|
|
|
|
|
|
pub use git::Git;
|
|
|
|
|
|
|
|
|
@ -184,7 +184,7 @@ impl<'a> GitView<'a> {
|
|
|
|
|
match url::Url::parse(git_url) {
|
|
|
|
|
Ok(url) => Ok(Url::new(
|
|
|
|
|
url.scheme(),
|
|
|
|
|
Domain::from_str(url.host_str().map_or_else(|| "github.com", |host| host)),
|
|
|
|
|
url.host_str().map_or_else(|| "github.com", |host| host),
|
|
|
|
|
url.path()
|
|
|
|
|
.trim_start_matches('/')
|
|
|
|
|
.trim_end_matches('/')
|
|
|
|
@ -205,7 +205,7 @@ impl<'a> GitView<'a> {
|
|
|
|
|
None => domain,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Ok(Url::new(protocol, Domain::from_str(split_domain), path))
|
|
|
|
|
Ok(Url::new(protocol, split_domain, path))
|
|
|
|
|
}
|
|
|
|
|
None => Err(AppError::new(
|
|
|
|
|
ErrorType::InvalidGitUrl,
|
|
|
|
@ -239,17 +239,10 @@ impl<'a> GitView<'a> {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handle issue flag and no flags
|
|
|
|
|
let branch_ref = match &url.domain {
|
|
|
|
|
Domain::GitHub => {
|
|
|
|
|
if self.is_issue {
|
|
|
|
|
let branch_ref = if self.is_issue {
|
|
|
|
|
format!("/issues/{}", capture_digits(remote_ref))
|
|
|
|
|
} else {
|
|
|
|
|
format!("/tree/{}", escape_ascii_chars(remote_ref))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Domain::BitBucket => {
|
|
|
|
|
format!("/src/{}", remote_ref)
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if remote_ref != "master" && remote_ref != "main" {
|
|
|
|
@ -725,7 +718,7 @@ mod lib_tests {
|
|
|
|
|
let url = handler.parse_git_url(git_url)?;
|
|
|
|
|
|
|
|
|
|
assert_eq!(url.protocol, "https");
|
|
|
|
|
assert_eq!(url.domain.to_string(), "github.com");
|
|
|
|
|
assert_eq!(url.domain, "github.com");
|
|
|
|
|
assert_eq!(url.path, "sgoudham/git-view");
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
@ -740,7 +733,7 @@ mod lib_tests {
|
|
|
|
|
let url = handler.parse_git_url(git_url)?;
|
|
|
|
|
|
|
|
|
|
assert_eq!(url.protocol, "https");
|
|
|
|
|
assert_eq!(url.domain.to_string(), "github.com");
|
|
|
|
|
assert_eq!(url.domain, "github.com");
|
|
|
|
|
assert_eq!(url.path, "sgoudham/git-view");
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
@ -763,7 +756,7 @@ mod lib_tests {
|
|
|
|
|
|
|
|
|
|
mod generate_final_url {
|
|
|
|
|
use crate::{
|
|
|
|
|
git::{Domain, GitOutput, MockGitTrait, Url},
|
|
|
|
|
git::{GitOutput, MockGitTrait, Url},
|
|
|
|
|
GitView,
|
|
|
|
|
};
|
|
|
|
|
use test_case::test_case;
|
|
|
|
@ -771,7 +764,7 @@ mod lib_tests {
|
|
|
|
|
#[test]
|
|
|
|
|
fn is_latest_commit() {
|
|
|
|
|
let handler = GitView::builder().with_commit("current").build();
|
|
|
|
|
let url = Url::new("https", Domain::GitHub, "sgoudham/git-view");
|
|
|
|
|
let url = Url::new("https", "github.com", "sgoudham/git-view");
|
|
|
|
|
let expected_final_url = "https://github.com/sgoudham/git-view/tree/commit_hash";
|
|
|
|
|
let mut mock = MockGitTrait::default();
|
|
|
|
|
|
|
|
|
@ -789,7 +782,7 @@ mod lib_tests {
|
|
|
|
|
let handler = GitView::builder()
|
|
|
|
|
.with_commit("8s2jl250as7f234jasfjj")
|
|
|
|
|
.build();
|
|
|
|
|
let url = Url::new("https", Domain::GitHub, "sgoudham/git-view");
|
|
|
|
|
let url = Url::new("https", "github.com", "sgoudham/git-view");
|
|
|
|
|
let expected_final_url =
|
|
|
|
|
"https://github.com/sgoudham/git-view/tree/8s2jl250as7f234jasfjj";
|
|
|
|
|
let mock = MockGitTrait::default();
|
|
|
|
@ -800,37 +793,11 @@ mod lib_tests {
|
|
|
|
|
assert_eq!(actual_final_url.unwrap(), expected_final_url);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn is_bitbucket() {
|
|
|
|
|
let handler = GitView::default();
|
|
|
|
|
let url = Url::new("https", Domain::BitBucket, "sgoudham/git-view");
|
|
|
|
|
let expected_final_url = "https://bitbucket.org/sgoudham/git-view/src/dev";
|
|
|
|
|
let mock = MockGitTrait::default();
|
|
|
|
|
|
|
|
|
|
let actual_final_url = handler.generate_final_url("dev", &url, &mock);
|
|
|
|
|
|
|
|
|
|
assert!(actual_final_url.is_ok());
|
|
|
|
|
assert_eq!(actual_final_url.unwrap(), expected_final_url);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn is_github() {
|
|
|
|
|
let handler = GitView::default();
|
|
|
|
|
let url = Url::new("https", Domain::GitHub, "sgoudham/git-view");
|
|
|
|
|
let expected_final_url = "https://github.com/sgoudham/git-view/tree/dev";
|
|
|
|
|
let mock = MockGitTrait::default();
|
|
|
|
|
|
|
|
|
|
let actual_final_url = handler.generate_final_url("dev", &url, &mock);
|
|
|
|
|
|
|
|
|
|
assert!(actual_final_url.is_ok());
|
|
|
|
|
assert_eq!(actual_final_url.unwrap(), expected_final_url);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test_case("main" ; "main")]
|
|
|
|
|
#[test_case("master" ; "master")]
|
|
|
|
|
fn is_master_or_main(branch: &str) {
|
|
|
|
|
let handler = GitView::default();
|
|
|
|
|
let url = Url::new("https", Domain::GitHub, "sgoudham/git-view");
|
|
|
|
|
let url = Url::new("https", "github.com", "sgoudham/git-view");
|
|
|
|
|
let expected_final_url = "https://github.com/sgoudham/git-view";
|
|
|
|
|
let mock = MockGitTrait::default();
|
|
|
|
|
|
|
|
|
@ -843,7 +810,7 @@ mod lib_tests {
|
|
|
|
|
#[test]
|
|
|
|
|
fn is_user_issue() {
|
|
|
|
|
let handler = GitView::builder().with_issue(true).build();
|
|
|
|
|
let url = Url::new("https", Domain::GitHub, "sgoudham/git-view");
|
|
|
|
|
let url = Url::new("https", "github.com", "sgoudham/git-view");
|
|
|
|
|
let expected_final_url = "https://github.com/sgoudham/git-view/issues/1234";
|
|
|
|
|
let mock = MockGitTrait::default();
|
|
|
|
|
|
|
|
|
@ -856,7 +823,7 @@ mod lib_tests {
|
|
|
|
|
#[test]
|
|
|
|
|
fn is_normal_branch() {
|
|
|
|
|
let handler = GitView::builder().with_issue(false).build();
|
|
|
|
|
let url = Url::new("https", Domain::GitHub, "sgoudham/git-view");
|
|
|
|
|
let url = Url::new("https", "github.com", "sgoudham/git-view");
|
|
|
|
|
let expected_final_url = "https://github.com/sgoudham/git-view/tree/%23test%23";
|
|
|
|
|
let mock = MockGitTrait::default();
|
|
|
|
|
|
|
|
|
@ -870,7 +837,7 @@ mod lib_tests {
|
|
|
|
|
#[test_case("dev", "https://github.com/sgoudham/git-view/tree/dev/releases" ; "with_branch_dev")]
|
|
|
|
|
fn with_suffix(remote_ref: &str, expected_final_url: &str) {
|
|
|
|
|
let handler = GitView::builder().with_suffix("/releases").build();
|
|
|
|
|
let url = Url::new("https", Domain::GitHub, "sgoudham/git-view");
|
|
|
|
|
let url = Url::new("https", "github.com", "sgoudham/git-view");
|
|
|
|
|
let mock = MockGitTrait::default();
|
|
|
|
|
|
|
|
|
|
let actual_final_url = handler.generate_final_url(remote_ref, &url, &mock);
|
|
|
|
|