class Git::Browse::Remote::Core

Constants

MAPPING_RECIPES

Attributes

file[RW]
lines[RW]

Public Instance Methods

_commit(short = false) click to toggle source
# File lib/git/browse/remote/core.rb, line 140
def _commit(short = false)
  if short
    Git.parse_rev_short(target || 'HEAD')
  else
    Git.parse_rev(target || 'HEAD')
  end
end
_ref(short = false) click to toggle source
# File lib/git/browse/remote/core.rb, line 172
def _ref(short = false)
  if short
    @ref.sub(%r(^refs/), '').
         sub(%r(^heads/), '').
         sub(%r(^tags/), '').
         sub(%r(^remotes/([^/]+)/), '')
  else
    @ref
  end
end
_rev(short = false) click to toggle source
# File lib/git/browse/remote/core.rb, line 156
def _rev(short = false)
  if mode == :rev
    _commit(short)
  else
    _ref(short) || _commit(short)
  end
end
commit() click to toggle source
# File lib/git/browse/remote/core.rb, line 148
def commit
  _commit
end
init!(host, name) click to toggle source
# File lib/git/browse/remote/core.rb, line 47
def init!(host, name)
  mapping = MAPPING_RECIPES[name] or abort "Recipe '#{name}' not found"
  mapping.each do |mode,template|
    system %Q(git config --global browse-remote.#{host}.#{mode} '#{template}')
  end
end
line() click to toggle source
# File lib/git/browse/remote/core.rb, line 191
def line
  lines and lines.first
end
mode() click to toggle source
# File lib/git/browse/remote/core.rb, line 126
def mode
  return @mode if @mode

  if ref
    if ref.match(%r<^((?:refs/)?(?:heads|remotes/[^/]+)/)?master$>)
      @mode = :top
    elsif ref.match(%r<^(?:refs/)?(?:heads|tags|remotes)/>)
      @mode = :ref
    end
  end

  @mode || :rev
end
mode=(mode) click to toggle source
# File lib/git/browse/remote/core.rb, line 122
def mode=(mode)
  @mode = mode
end
ref() click to toggle source
# File lib/git/browse/remote/core.rb, line 183
def ref
  _ref
end
remote() click to toggle source
# File lib/git/browse/remote/core.rb, line 110
def remote
  return @remote if @remote

  if ref
    if ref.match(%r<^(?:refs/)?remotes/([^/]+)/>)
      @remote = $1
    end
  end

  @remote ||= 'origin'
end
remote=(remote) click to toggle source
# File lib/git/browse/remote/core.rb, line 106
def remote=(remote)
  @remote = remote
end
rev() click to toggle source
# File lib/git/browse/remote/core.rb, line 164
def rev
  _rev
end
short_commit() click to toggle source
# File lib/git/browse/remote/core.rb, line 152
def short_commit
  _commit(true)
end
short_ref() click to toggle source
# File lib/git/browse/remote/core.rb, line 187
def short_ref
  _ref(true)
end
short_rev() click to toggle source
# File lib/git/browse/remote/core.rb, line 168
def short_rev
  _rev(true)
end
target() click to toggle source
# File lib/git/browse/remote/core.rb, line 199
def target
  return @target if @target
  @target ||= Git.resolved_head
end
target=(target) click to toggle source
# File lib/git/browse/remote/core.rb, line 195
def target=(target)
  @target = target
end
template_type() click to toggle source
# File lib/git/browse/remote/core.rb, line 39
def template_type
  if @file
    :file
  else
    mode
  end
end
url() click to toggle source
# File lib/git/browse/remote/core.rb, line 54
def url
  if target && !@file && File.exists?(target)
    self.target, @file = nil, target
  end

  if @file && File.exists?(@file)
    @file = Filepath.new(@file)
  end

  if target
    if Git.is_valid_rev? target
      @ref = Git.full_name_of_rev(target)
    elsif Git.is_valid_remote? target
      @remote, @ref = target, 'master'
    else
      abort "Not a valid ref or remote: #{target}"
    end
  else
    @ref = Git.symbolic_name_of_head
  end

  if @ref == 'HEAD'
    @ref = nil
  end

  remote_url = `git config remote.#{remote}.url`[/.+/] or
      abort "Could not get remote url: #{remote}"

  unless %r(^\w+://) === remote_url
    # SCP-like URLs of form "[user@]host.xz:path/to/repo.git"
    # will be converted to "ssh://[user@]host.xz/path/to/repo.git"
    remote_url = 'ssh://' + remote_url.sub(/:/, '/')
  end

  # Normal URLs
  u = URI(remote_url)
  host = u.host
  port = u.port
  host_port = if port == u.default_port then host else "#{host}:#{port}" end
  path = u.path.sub(%r(^/), '').split('/')
  path.last.sub!(/\.git$/, '')
  path = Path.new(path)

  template = `git config --get browse-remote.#{host}.#{template_type}`[/.+/]
  if not template and host == 'github.com'
    template = MAPPING_RECIPES[:github][template_type.to_sym] or
      abort "No '#{template_type}' mapping found for #{host} (maybe `git browse-remote --init` required)"
  end

  url = template.gsub(/\{(.+?)\}/) { |m| eval($1) }
end