Class: Git::Browse::Remote::Core

Inherits:
Object
  • Object
show all
Defined in:
lib/git/browse/remote/core.rb

Constant Summary collapse

MAPPING_RECIPES =
{
  :github => {
    :top  => 'https://{host}/{path}',
    :ref  => 'https://{host}/{path}/tree/{short_ref}',
    :rev  => 'https://{host}/{path}/commit/{commit}',
    :file => 'https://{host}/{path}/blob/{short_rev}/{file}{line && "#L%d" % line}'
  },

  :gitweb => {
    :top => 'http://{host}/?p={path[-2..-1]}.git',
    :ref => 'http://{host}/?p={path[-2..-1]}.git;h={ref}',
    :rev => 'http://{host}/?p={path[-2..-1]}.git;a=commit;h={ref}',
    # XXX
    # I don't know file url of gitweb...
  }
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fileObject

Returns the value of attribute file.



11
12
13
# File 'lib/git/browse/remote/core.rb', line 11

def file
  @file
end

#lineObject

Returns the value of attribute line.



11
12
13
# File 'lib/git/browse/remote/core.rb', line 11

def line
  @line
end

Instance Method Details

#_commit(short = false) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/git/browse/remote/core.rb', line 116

def _commit(short = false)
  if short
    Git.parse_rev_short(target || 'HEAD')
  else
    Git.parse_rev(target || 'HEAD')
  end
end

#_ref(short = false) ⇒ Object



148
149
150
151
152
153
154
155
156
157
# File 'lib/git/browse/remote/core.rb', line 148

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) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/git/browse/remote/core.rb', line 132

def _rev(short = false)
  if mode == :rev
    _commit(short)
  else
    _ref(short) || _commit(short)
  end
end

#commitObject



124
125
126
# File 'lib/git/browse/remote/core.rb', line 124

def commit
  _commit
end

#init!(host, name) ⇒ Object



38
39
40
41
42
43
# File 'lib/git/browse/remote/core.rb', line 38

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

#modeObject



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/git/browse/remote/core.rb', line 102

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) ⇒ Object



98
99
100
# File 'lib/git/browse/remote/core.rb', line 98

def mode=(mode)
  @mode = mode
end

#refObject



159
160
161
# File 'lib/git/browse/remote/core.rb', line 159

def ref
  _ref
end

#remoteObject



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/git/browse/remote/core.rb', line 86

def remote
  return @remote if @remote

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

  @remote ||= 'origin'
end

#remote=(remote) ⇒ Object



82
83
84
# File 'lib/git/browse/remote/core.rb', line 82

def remote=(remote)
  @remote = remote
end

#revObject



140
141
142
# File 'lib/git/browse/remote/core.rb', line 140

def rev
  _rev
end

#short_commitObject



128
129
130
# File 'lib/git/browse/remote/core.rb', line 128

def short_commit
  _commit(true)
end

#short_refObject



163
164
165
# File 'lib/git/browse/remote/core.rb', line 163

def short_ref
  _ref(true)
end

#short_revObject



144
145
146
# File 'lib/git/browse/remote/core.rb', line 144

def short_rev
  _rev(true)
end

#targetObject



171
172
173
174
# File 'lib/git/browse/remote/core.rb', line 171

def target
  return @target if @target
  @target ||= Git.resolved_head
end

#target=(target) ⇒ Object



167
168
169
# File 'lib/git/browse/remote/core.rb', line 167

def target=(target)
  @target = target
end

#template_typeObject



30
31
32
33
34
35
36
# File 'lib/git/browse/remote/core.rb', line 30

def template_type
  if @file
    :file
  else
    mode
  end
end

#urlObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/git/browse/remote/core.rb', line 45

def url
  if target && !@file && File.exists?(target)
    self.target, @file = nil, target
  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}"

  host, *path = remote_url.sub(%r(^\w+://), '').sub(/^[\w-]+@/, '').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