Skip to content

Instantly share code, notes, and snippets.

@bjjb
Created September 7, 2022 16:02
Show Gist options
  • Save bjjb/8dd3018ba2fb2400ad5b943250752d46 to your computer and use it in GitHub Desktop.
Save bjjb/8dd3018ba2fb2400ad5b943250752d46 to your computer and use it in GitHub Desktop.
#!/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
{
'git@github.com:foo' => 'github.com foo',
'git@github.com:foo/bar' => 'github.com foo/bar',
'git@gitlab.com:foo/bar/baz' => 'gitlab.com foo/bar/baz',
'git@gitlab.com: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