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}/{file.directory? and :tree or :blob}/{short_rev}/{file}{lines && "#L%s" % lines.join("-")}',
  },

  :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.



18
19
20
# File 'lib/git/browse/remote/core.rb', line 18

def file
  @file
end

#linesObject

Returns the value of attribute lines.



18
19
20
# File 'lib/git/browse/remote/core.rb', line 18

def lines
  @lines
end

Instance Method Details

#_commit(short = false) ⇒ Object



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

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

#_ref(short = false) ⇒ Object



159
160
161
162
163
164
165
166
167
168
# File 'lib/git/browse/remote/core.rb', line 159

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



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

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

#commitObject



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

def commit
  _commit
end

#init!(host, name) ⇒ Object



45
46
47
48
49
50
# File 'lib/git/browse/remote/core.rb', line 45

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

#lineObject



178
179
180
# File 'lib/git/browse/remote/core.rb', line 178

def line
  lines and lines.first
end

#modeObject



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/git/browse/remote/core.rb', line 113

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



109
110
111
# File 'lib/git/browse/remote/core.rb', line 109

def mode=(mode)
  @mode = mode
end

#refObject



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

def ref
  _ref
end

#remoteObject



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/git/browse/remote/core.rb', line 97

def remote
  return @remote if @remote

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

  @remote ||= 'origin'
end

#remote=(remote) ⇒ Object



93
94
95
# File 'lib/git/browse/remote/core.rb', line 93

def remote=(remote)
  @remote = remote
end

#revObject



151
152
153
# File 'lib/git/browse/remote/core.rb', line 151

def rev
  _rev
end

#short_commitObject



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

def short_commit
  _commit(true)
end

#short_refObject



174
175
176
# File 'lib/git/browse/remote/core.rb', line 174

def short_ref
  _ref(true)
end

#short_revObject



155
156
157
# File 'lib/git/browse/remote/core.rb', line 155

def short_rev
  _rev(true)
end

#targetObject



186
187
188
189
# File 'lib/git/browse/remote/core.rb', line 186

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

#target=(target) ⇒ Object



182
183
184
# File 'lib/git/browse/remote/core.rb', line 182

def target=(target)
  @target = target
end

#template_typeObject



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

def template_type
  if @file
    :file
  else
    mode
  end
end

#urlObject



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
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/git/browse/remote/core.rb', line 52

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}"

  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