Created
September 7, 2022 16:02
-
-
Save bjjb/8dd3018ba2fb2400ad5b943250752d46 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# frozen_string_literal: true | |
module ParseGitURL | |
SSH = /^(?:[^@]+)@([^:]+):(.+)/.freeze | |
HTTPS = %r{^https://([^/]+)/(.+)}.freeze | |
def parse(input) | |
case input | |
when SSH then "#{Regexp.last_match(1)} #{Regexp.last_match(2)}" | |
when HTTPS then "#{Regexp.last_match(1)} #{Regexp.last_match(2)}" | |
else '' | |
end.sub(/\.git$/, '') | |
end | |
end | |
if __FILE__ == $PROGRAM_NAME | |
require 'minitest/spec' | |
require 'minitest/autorun' | |
include ParseGitURL | |
{ | |
'[email protected]:foo' => 'github.com foo', | |
'[email protected]:foo/bar' => 'github.com foo/bar', | |
'[email protected]:foo/bar/baz' => 'gitlab.com foo/bar/baz', | |
'[email protected]:foo/bar/baz.git' => 'gitlab.com foo/bar/baz', | |
'me@localhost:foo/bar/baz' => 'localhost foo/bar/baz', | |
'https://github.com/foo' => 'github.com foo', | |
'https://github.com/foo/bar' => 'github.com foo/bar', | |
'https://github.com/foo/bar.git' => 'github.com foo/bar' | |
}.each do |input, output| | |
describe "parse('#{input}')" do | |
it '' do | |
_(parse(input)).must_equal output | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment