refactor(#4)!: Drop support for BitBucket URLs

BREAKING CHANGE: This removes support for BitBucket URLs
as I am not well versed with them to robustly support it

closes #4
pull/25/head
Hamothy 3 years ago committed by Hamothy
parent 4ea6a82bb1
commit f6a6c7f27c

@ -21,7 +21,6 @@ Are you _**also**_ frustrated from moving your hands away from the keyboard to v
## Features
- [x] GitHub & BitBucket
- [x] View Branches, Commits & Issues
- [x] Custom Suffix
- [x] Custom Remote

Binary file not shown.

Before

Width:  |  Height:  |  Size: 76 KiB

After

Width:  |  Height:  |  Size: 129 KiB

@ -1,6 +1,6 @@
use std::panic::set_hook;
use clap::{command, crate_authors, crate_version, Arg, Command, ErrorKind};
use clap::{command, crate_description, crate_authors, crate_version, Arg, Command, ErrorKind};
use git_view::Git;
use git_view::GitView;
@ -16,14 +16,7 @@ fn main() {
let matches = Command::new("git-view")
.version(crate_version!())
.author(crate_authors!())
.about(
"A git sub-command to view your git repository in the browser.
This currently supports the following URLs:
- github.com
- bitbucket.org
",
)
.about(crate_description!())
.arg(
Arg::new("remote")
.long_help("The remote to view git repository on\n[default: default remote]")

@ -1,4 +1,3 @@
use core::fmt;
use std::{
borrow::Cow,
process::{Command, Output},
@ -14,16 +13,10 @@ pub(crate) enum Local<'a> {
NotBranch,
}
#[derive(Debug)]
pub(crate) enum Domain {
GitHub,
BitBucket,
}
#[derive(Debug)]
pub(crate) struct Url {
pub(crate) protocol: String,
pub(crate) domain: Domain,
pub(crate) domain: String,
pub(crate) path: String,
}
@ -163,36 +156,11 @@ fn trim(bytes: &[u8]) -> Result<String, AppError> {
}
impl Url {
pub(crate) fn new(protocol: &str, domain: Domain, path: &str) -> Self {
pub(crate) fn new(protocol: &str, domain: &str, path: &str) -> Self {
Self {
protocol: protocol.into(),
domain,
domain: domain.into(),
path: path.into(),
}
}
}
impl Domain {
pub(crate) fn from_str(s: &str) -> Self {
if s == "bitbucket.org" {
Domain::BitBucket
} else {
Domain::GitHub
}
}
}
impl PartialEq for Domain {
fn eq(&self, other: &Self) -> bool {
self.to_string() == other.to_string()
}
}
impl fmt::Display for Domain {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Domain::GitHub => write!(f, "github.com"),
Domain::BitBucket => write!(f, "bitbucket.org"),
}
}
}

@ -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 {
format!("/issues/{}", capture_digits(remote_ref))
} else {
format!("/tree/{}", escape_ascii_chars(remote_ref))
}
}
Domain::BitBucket => {
format!("/src/{}", remote_ref)
}
let branch_ref = if self.is_issue {
format!("/issues/{}", capture_digits(remote_ref))
} else {
format!("/tree/{}", escape_ascii_chars(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);

Loading…
Cancel
Save