Skip to content

Commit

Permalink
Add Repo::Ref#base_url_source and #base_url_raw
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota committed May 2, 2020
1 parent 4b8220f commit 4e4972f
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
35 changes: 35 additions & 0 deletions spec/repo_spec.cr → spec/repo_ref_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,41 @@ describe Repo::Ref do
Repo::Ref.new("github", "foo/bar").to_uri.should eq URI.parse("https://github.com/foo/bar")
end


it "#base_url_source" do
Repo::Ref.new("github", "foo/bar").base_url_source.should eq URI.parse("https://github.com/foo/bar/tree/master/")
Repo::Ref.new("github", "foo/bar").base_url_source("HEAD").should eq URI.parse("https://github.com/foo/bar/tree/master/")
Repo::Ref.new("github", "foo/bar").base_url_source("12345").should eq URI.parse("https://github.com/foo/bar/tree/12345/")

Repo::Ref.new("gitlab", "foo/bar").base_url_source.should eq URI.parse("https://gitlab.com/foo/bar/-/tree/master/")
Repo::Ref.new("gitlab", "foo/bar").base_url_source("HEAD").should eq URI.parse("https://gitlab.com/foo/bar/-/tree/master/")
Repo::Ref.new("gitlab", "foo/bar").base_url_source("12345").should eq URI.parse("https://gitlab.com/foo/bar/-/tree/12345/")

Repo::Ref.new("bitbucket", "foo/bar").base_url_source.should eq URI.parse("https://bitbucket.com/foo/bar/src/master/")
Repo::Ref.new("bitbucket", "foo/bar").base_url_source("HEAD").should eq URI.parse("https://bitbucket.com/foo/bar/src/master/")
Repo::Ref.new("bitbucket", "foo/bar").base_url_source("12345").should eq URI.parse("https://bitbucket.com/foo/bar/src/12345/")

Repo::Ref.new("git", "foo/bar").base_url_source.should be_nil
Repo::Ref.new("git", "foo/bar").base_url_source("12345").should be_nil
end

it "#base_url_raw" do
Repo::Ref.new("github", "foo/bar").base_url_raw.should eq URI.parse("https://github.com/foo/bar/raw/master/")
Repo::Ref.new("github", "foo/bar").base_url_raw("HEAD").should eq URI.parse("https://github.com/foo/bar/raw/master/")
Repo::Ref.new("github", "foo/bar").base_url_raw("12345").should eq URI.parse("https://github.com/foo/bar/raw/12345/")

Repo::Ref.new("gitlab", "foo/bar").base_url_raw.should eq URI.parse("https://gitlab.com/foo/bar/-/raw/master/")
Repo::Ref.new("gitlab", "foo/bar").base_url_raw("HEAD").should eq URI.parse("https://gitlab.com/foo/bar/-/raw/master/")
Repo::Ref.new("gitlab", "foo/bar").base_url_raw("12345").should eq URI.parse("https://gitlab.com/foo/bar/-/raw/12345/")

Repo::Ref.new("bitbucket", "foo/bar").base_url_raw.should eq URI.parse("https://bitbucket.com/foo/bar/raw/master/")
Repo::Ref.new("bitbucket", "foo/bar").base_url_raw("HEAD").should eq URI.parse("https://bitbucket.com/foo/bar/raw/master/")
Repo::Ref.new("bitbucket", "foo/bar").base_url_raw("12345").should eq URI.parse("https://bitbucket.com/foo/bar/raw/12345/")

Repo::Ref.new("git", "foo/bar").base_url_raw.should be_nil
Repo::Ref.new("git", "foo/bar").base_url_raw("12345").should be_nil
end

it "#nice_url" do
Repo::Ref.new("file:https:///repo.git").nice_url.should eq "file:https:///repo.git"
Repo::Ref.new("file:https:///repo").nice_url.should eq "file:https:///repo"
Expand Down
51 changes: 51 additions & 0 deletions src/repo/ref.cr
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,57 @@ struct Repo::Ref
end
end

def base_url_source(refname = nil)
refname = normalize_refname(refname)

case resolver
when "bitbucket"
url = to_uri
url.path += "/src/#{refname}/"
url
when "github"
url = to_uri
url.path += "/tree/#{refname}/"
url
when "gitlab"
# gitlab doesn't necessarily need the `-` component but they use it by default
# and it seems reasonable to be safe of any ambiguities
url = to_uri
url.path += "/-/tree/#{refname}/"
url
else
nil
end
end

def base_url_raw(refname = nil)
refname = normalize_refname(refname)

case resolver
when "github", "bitbucket"
url = to_uri
url.path += "/raw/#{refname}/"
url
when "gitlab"
# gitlab doesn't necessarily need the `-` component but they use it by default
# and it seems reasonable to be safe of any ambiguities
url = to_uri
url.path += "/-/raw/#{refname}/"
url
else
nil
end
end

private def normalize_refname(refname)
case refname
when Nil, "HEAD"
"master"
else
refname
end
end

def resolvable?
provider_resolver? || url.starts_with?("http:https://") || url.starts_with?("https://")
end
Expand Down

0 comments on commit 4e4972f

Please sign in to comment.