class GitInit::GitUrl

Attributes

basename[RW]
domain[RW]
path[RW]
proto[RW]
type[RW]
user[RW]

Public Class Methods

detect_type(url) click to toggle source
# File lib/git_init.rb, line 72
def GitUrl.detect_type(url)

  # Simple git url
  return :git if (/^git:\/\//.match(url))

  # http(s)
  return :http if (/^https?:\/\//.match(url))

  # ssh
  return :ssh if (/:/.match(url))

  raise "Unknown type of url #{url}"

end
new(opts={}) click to toggle source
# File lib/git_init.rb, line 11
def initialize(opts={})

  # convert url if needed
  if opts.is_a?(String)
    opts = GitUrl.parse_url(opts)
  end

  # set instance vars
  opts.each do |key, value|
    self.send("#{key}=".to_sym, value)
  end
end
parse_url(url) click to toggle source
# File lib/git_init.rb, line 40
def GitUrl.parse_url(url)

  ret_val = {}

  # Detect type
  ret_val[:type] = detect_type(url)

  # Parse ssh url
  if ret_val[:type] == :ssh
    m = /^(([^@]+)@)?([^:]+):(.*\.git$)/.match(url)
    ret_val[:user] = m[2]
    ret_val[:domain] = m[3]
    ret_val[:path] = File.dirname(m[4])
    ret_val[:basename] = File.basename(m[4])
  elsif ret_val[:type] == :http
    m = /^(https?):\/\/([^\/]+)\/(.*\.git$)/.match(url)
    ret_val[:proto] = m[1]
    ret_val[:domain] = m[2]
    ret_val[:path] = File.dirname(m[3])
    ret_val[:basename] = File.basename(m[3])
  elsif ret_val[:type] == :git
    m = /^git:\/\/([^\/]+)\/(.*\.git$)/.match(url)
    ret_val[:domain] = m[1]
    ret_val[:path] = File.dirname(m[2])
    ret_val[:basename] = File.basename(m[2])
  end


  return ret_val

end

Public Instance Methods

url() click to toggle source
# File lib/git_init.rb, line 24
def url
  if type == :http
    "#{self.proto}://#{self.domain}/#{File.join(self.path,self.basename)}"
  elsif type == :ssh
    if self.user.nil?
      user = ""
    else
      user = "#{self.user}@"
    end
    "#{user}#{self.domain}:#{File.join(self.path,self.basename)}"
  elsif type == :git
    "git://#{self.domain}/#{File.join(self.path,self.basename)}"
  end
end