Skip to content

Commit

Permalink
fix: open repository with - in domain
Browse files Browse the repository at this point in the history
closes #62
  • Loading branch information
yoannfleurydev committed Apr 17, 2021
1 parent f2ccb2d commit 06174db
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 41 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.3.1] - 2021-04-17

### Changed

- update the code to get the remote parts
- remove the quircky regex

## [0.3.0] - 2021-01-30

### Added
Expand Down
50 changes: 47 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 18 additions & 17 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
[package]
name = "gitweb"
version = "0.3.0"
authors = ["Yoann Fleury <yoann.fleury@yahoo.com>"]
edition = "2018"
categories = ["command-line-utilities"]
description = "Open the current remote repository in your browser"
repository = "https://github.com/yoannfleurydev/gitweb"
edition = "2018"
exclude = [
"docs/",
".editorconfig",
".github/",
"*.md",
]
homepage = "https://github.com/yoannfleurydev/gitweb"
readme = "README.md"
keywords = ["git", "browser"]
categories = ["command-line-utilities"]
license = "Apache-2.0"
exclude = [
"docs/",
".editorconfig",
".github/",
"*.md"
]
name = "gitweb"
readme = "README.md"
repository = "https://github.com/yoannfleurydev/gitweb"
version = "0.3.1"

[dependencies]
regex = "1"
structopt = "0.3"
open = "1.6"
anyhow = "1.0"
flexi_logger = "0.17"
git-url-parse = "0.3"
git2 = "0.13"
log = "0.4"
flexi_logger = "0.17"
anyhow = "1.0"
open = "1.6"
regex = "1"
structopt = "0.3"
thiserror = "1.0"
47 changes: 26 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use regex::Regex;
use git_url_parse::GitUrl;
use thiserror::Error;

use crate::options::Opt;
Expand Down Expand Up @@ -87,19 +87,19 @@ struct MergeRequestParts {
const DEFAULT_REMOTE_ORIGIN: &str = "origin";

fn get_remote_parts(url: &str) -> anyhow::Result<RemoteParts> {
let re: Regex = Regex::new(r"((\w+://)|(git@))(.+@)*(?P<domain>[\w\d.]+)(:[\d]+)?/*(:?)(?P<repository>[^.]*)(\.git)?(/)?$").unwrap();
let giturl = GitUrl::parse(url).map_err(|_| Issue::UnableToGetRemoteParts)?;

let caps = re
.captures(url)
.ok_or_else(|| ())
.map_err(|_| Issue::UnableToGetRemoteParts)?;

let domain = caps
.name("domain")
let domain = giturl
.host
.map_or(GitProvider::GitHub.hostname(), |m| m.as_str().to_string());
let repository = caps
.name("repository")
.map_or("".to_string(), |m| m.as_str().to_string());

let repository = giturl
.path
.replace(".git", "") // don't want the .git part
.split("/")
.filter(|s| !s.is_empty())
.collect::<Vec<&str>>()
.join("/");

Ok(RemoteParts { domain, repository })
}
Expand All @@ -122,7 +122,7 @@ fn get_merge_request_parts(domain: &str) -> anyhow::Result<MergeRequestParts, Is
path: "pulls".to_string(),
tail: "".to_string(),
}),
_ => Err(Issue::UnknownProvider)
_ => Err(Issue::UnknownProvider),
}
}

Expand Down Expand Up @@ -280,6 +280,15 @@ mod tests {
assert_eq!(repository, "path/to/repo");
}

#[test]
fn test_with_http_dash_and_port_git_url_parts() {
let RemoteParts { domain, repository } =
get_remote_parts("http://host-dash.xz:80/path/to/repo.git/").unwrap();

assert_eq!(domain, "host-dash.xz");
assert_eq!(repository, "path/to/repo");
}

#[test]
fn test_with_http_git_url_parts() {
let RemoteParts { domain, repository } =
Expand All @@ -291,35 +300,31 @@ mod tests {

#[test]
fn test_get_merge_request_parts_with_github() {
let MergeRequestParts { path, tail } =
get_merge_request_parts(GITHUB_HOSTNAME).unwrap();
let MergeRequestParts { path, tail } = get_merge_request_parts(GITHUB_HOSTNAME).unwrap();

assert_eq!(path, "pulls");
assert_eq!(tail, "");
}

#[test]
fn test_get_merge_request_parts_with_gitlab() {
let MergeRequestParts { path, tail } =
get_merge_request_parts(GITLAB_HOSTNAME).unwrap();
let MergeRequestParts { path, tail } = get_merge_request_parts(GITLAB_HOSTNAME).unwrap();

assert_eq!(path, "-/merge_requests");
assert_eq!(tail, "");
}

#[test]
fn test_get_merge_request_parts_with_bitbucket() {
let MergeRequestParts { path, tail } =
get_merge_request_parts(BITBUCKET_HOSTNAME).unwrap();
let MergeRequestParts { path, tail } = get_merge_request_parts(BITBUCKET_HOSTNAME).unwrap();

assert_eq!(path, "pull-requests");
assert_eq!(tail, "");
}

#[test]
fn test_get_merge_request_parts_with_gitea() {
let MergeRequestParts { path, tail } =
get_merge_request_parts(GITEA_HOSTNAME).unwrap();
let MergeRequestParts { path, tail } = get_merge_request_parts(GITEA_HOSTNAME).unwrap();

assert_eq!(path, "pulls");
assert_eq!(tail, "");
Expand Down

0 comments on commit 06174db

Please sign in to comment.