annotate app/helpers/repositories_helper.rb @ 1169:492ff72268e3 bug_521

Close obsolete branch bug_521
author Chris Cannam
date Thu, 18 Oct 2012 10:42:48 +0100
parents 5e80956cc792
children bb32da3bea34
rev   line source
Chris@909 1 # encoding: utf-8
Chris@909 2 #
Chris@441 3 # Redmine - project management software
Chris@441 4 # Copyright (C) 2006-2011 Jean-Philippe Lang
Chris@0 5 #
Chris@0 6 # This program is free software; you can redistribute it and/or
Chris@0 7 # modify it under the terms of the GNU General Public License
Chris@0 8 # as published by the Free Software Foundation; either version 2
Chris@0 9 # of the License, or (at your option) any later version.
Chris@441 10 #
Chris@0 11 # This program is distributed in the hope that it will be useful,
Chris@0 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 14 # GNU General Public License for more details.
Chris@441 15 #
Chris@0 16 # You should have received a copy of the GNU General Public License
Chris@0 17 # along with this program; if not, write to the Free Software
Chris@0 18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 19
Chris@0 20 require 'iconv'
Chris@441 21 require 'redmine/codeset_util'
Chris@0 22
Chris@0 23 module RepositoriesHelper
Chris@119 24 def format_revision(revision)
Chris@119 25 if revision.respond_to? :format_identifier
Chris@119 26 revision.format_identifier
Chris@119 27 else
Chris@119 28 revision.to_s
Chris@119 29 end
Chris@0 30 end
Chris@245 31
Chris@0 32 def truncate_at_line_break(text, length = 255)
Chris@0 33 if text
Chris@0 34 text.gsub(%r{^(.{#{length}}[^\n]*)\n.+$}m, '\\1...')
Chris@0 35 end
Chris@0 36 end
Chris@245 37
Chris@0 38 def render_properties(properties)
Chris@0 39 unless properties.nil? || properties.empty?
Chris@0 40 content = ''
Chris@0 41 properties.keys.sort.each do |property|
Chris@909 42 content << content_tag('li', "<b>#{h property}</b>: <span>#{h properties[property]}</span>".html_safe)
Chris@0 43 end
Chris@909 44 content_tag('ul', content.html_safe, :class => 'properties')
Chris@0 45 end
Chris@0 46 end
Chris@245 47
Chris@0 48 def render_changeset_changes
Chris@0 49 changes = @changeset.changes.find(:all, :limit => 1000, :order => 'path').collect do |change|
Chris@0 50 case change.action
Chris@0 51 when 'A'
Chris@0 52 # Detects moved/copied files
Chris@0 53 if !change.from_path.blank?
Chris@441 54 change.action =
Chris@441 55 @changeset.changes.detect {|c| c.action == 'D' && c.path == change.from_path} ? 'R' : 'C'
Chris@0 56 end
Chris@0 57 change
Chris@0 58 when 'D'
Chris@0 59 @changeset.changes.detect {|c| c.from_path == change.path} ? nil : change
Chris@0 60 else
Chris@0 61 change
Chris@0 62 end
Chris@441 63 end.compact
Chris@441 64
Chris@0 65 tree = { }
Chris@0 66 changes.each do |change|
Chris@0 67 p = tree
Chris@0 68 dirs = change.path.to_s.split('/').select {|d| !d.blank?}
Chris@0 69 path = ''
Chris@0 70 dirs.each do |dir|
Chris@0 71 path += '/' + dir
Chris@0 72 p[:s] ||= {}
Chris@0 73 p = p[:s]
Chris@0 74 p[path] ||= {}
Chris@0 75 p = p[path]
Chris@0 76 end
Chris@0 77 p[:c] = change
Chris@0 78 end
Chris@0 79 render_changes_tree(tree[:s])
Chris@0 80 end
Chris@245 81
Chris@0 82 def render_changes_tree(tree)
Chris@0 83 return '' if tree.nil?
Chris@0 84 output = ''
Chris@0 85 output << '<ul>'
Chris@0 86 tree.keys.sort.each do |file|
Chris@0 87 style = 'change'
Chris@0 88 text = File.basename(h(file))
Chris@0 89 if s = tree[file][:s]
Chris@0 90 style << ' folder'
Chris@0 91 path_param = to_path_param(@repository.relative_path(file))
Chris@909 92 text = link_to(h(text), :controller => 'repositories',
Chris@0 93 :action => 'show',
Chris@0 94 :id => @project,
Chris@0 95 :path => path_param,
Chris@119 96 :rev => @changeset.identifier)
Chris@0 97 output << "<li class='#{style}'>#{text}</li>"
Chris@0 98 output << render_changes_tree(s)
Chris@0 99 elsif c = tree[file][:c]
Chris@0 100 style << " change-#{c.action}"
Chris@0 101 path_param = to_path_param(@repository.relative_path(c.path))
Chris@909 102 text = link_to(h(text), :controller => 'repositories',
Chris@0 103 :action => 'entry',
Chris@0 104 :id => @project,
Chris@0 105 :path => path_param,
Chris@119 106 :rev => @changeset.identifier) unless c.action == 'D'
Chris@909 107 text << " - #{h(c.revision)}" unless c.revision.blank?
Chris@909 108 text << ' ('.html_safe + link_to(l(:label_diff), :controller => 'repositories',
Chris@0 109 :action => 'diff',
Chris@0 110 :id => @project,
Chris@0 111 :path => path_param,
Chris@909 112 :rev => @changeset.identifier) + ') '.html_safe if c.action == 'M'
Chris@909 113 text << ' '.html_safe + content_tag('span', h(c.from_path), :class => 'copied-from') unless c.from_path.blank?
Chris@0 114 output << "<li class='#{style}'>#{text}</li>"
Chris@0 115 end
Chris@0 116 end
Chris@0 117 output << '</ul>'
Chris@909 118 output.html_safe
Chris@0 119 end
Chris@245 120
Chris@245 121 def repository_field_tags(form, repository)
Chris@0 122 method = repository.class.name.demodulize.underscore + "_field_tags"
Chris@245 123 if repository.is_a?(Repository) &&
Chris@245 124 respond_to?(method) && method != 'repository_field_tags'
Chris@245 125 send(method, form, repository)
Chris@245 126 end
Chris@0 127 end
Chris@245 128
Chris@0 129 def scm_select_tag(repository)
Chris@0 130 scm_options = [["--- #{l(:actionview_instancetag_blank_option)} ---", '']]
Chris@0 131 Redmine::Scm::Base.all.each do |scm|
Chris@245 132 if Setting.enabled_scm.include?(scm) ||
Chris@245 133 (repository && repository.class.name.demodulize == scm)
Chris@245 134 scm_options << ["Repository::#{scm}".constantize.scm_name, scm]
Chris@245 135 end
Chris@0 136 end
Chris@441 137 select_tag('repository_scm',
Chris@0 138 options_for_select(scm_options, repository.class.name.demodulize),
Chris@0 139 :disabled => (repository && !repository.new_record?),
Chris@245 140 :onchange => remote_function(
Chris@245 141 :url => {
Chris@245 142 :controller => 'repositories',
Chris@441 143 :action => 'edit',
Chris@441 144 :id => @project
Chris@441 145 },
Chris@245 146 :method => :get,
Chris@441 147 :with => "Form.serialize(this.form)")
Chris@441 148 )
Chris@0 149 end
Chris@245 150
Chris@0 151 def with_leading_slash(path)
Chris@0 152 path.to_s.starts_with?('/') ? path : "/#{path}"
Chris@0 153 end
Chris@245 154
Chris@0 155 def without_leading_slash(path)
Chris@0 156 path.gsub(%r{^/+}, '')
Chris@0 157 end
Chris@0 158
Chris@0 159 def subversion_field_tags(form, repository)
Chris@245 160 content_tag('p', form.text_field(:url, :size => 60, :required => true,
Chris@245 161 :disabled => (repository && !repository.root_url.blank?)) +
Chris@909 162 '<br />'.html_safe +
Chris@909 163 '(file:///, http://, https://, svn://, svn+[tunnelscheme]://)') +
Chris@0 164 content_tag('p', form.text_field(:login, :size => 30)) +
Chris@245 165 content_tag('p', form.password_field(
Chris@245 166 :password, :size => 30, :name => 'ignore',
Chris@245 167 :value => ((repository.new_record? || repository.password.blank?) ? '' : ('x'*15)),
Chris@245 168 :onfocus => "this.value=''; this.name='repository[password]';",
Chris@245 169 :onchange => "this.name='repository[password]';"))
Chris@0 170 end
Chris@0 171
Chris@0 172 def darcs_field_tags(form, repository)
Chris@441 173 content_tag('p', form.text_field(
Chris@441 174 :url, :label => l(:field_path_to_repository),
Chris@441 175 :size => 60, :required => true,
Chris@441 176 :disabled => (repository && !repository.new_record?))) +
Chris@441 177 content_tag('p', form.select(
Chris@441 178 :log_encoding, [nil] + Setting::ENCODINGS,
Chris@441 179 :label => l(:field_commit_logs_encoding), :required => true))
Chris@0 180 end
Chris@245 181
Chris@0 182 def mercurial_field_tags(form, repository)
Chris@441 183 content_tag('p', form.text_field(
Chris@441 184 :url, :label => l(:field_path_to_repository),
Chris@245 185 :size => 60, :required => true,
Chris@441 186 :disabled => (repository && !repository.root_url.blank?)
Chris@441 187 ) +
Chris@909 188 '<br />'.html_safe + l(:text_mercurial_repository_note)) +
Chris@441 189 content_tag('p', form.select(
Chris@441 190 :path_encoding, [nil] + Setting::ENCODINGS,
Chris@441 191 :label => l(:field_scm_path_encoding)
Chris@441 192 ) +
Chris@909 193 '<br />'.html_safe + l(:text_scm_path_encoding_note))
Chris@0 194 end
Chris@0 195
Chris@0 196 def git_field_tags(form, repository)
Chris@441 197 content_tag('p', form.text_field(
Chris@441 198 :url, :label => l(:field_path_to_repository),
Chris@245 199 :size => 60, :required => true,
Chris@441 200 :disabled => (repository && !repository.root_url.blank?)
Chris@441 201 ) +
Chris@909 202 '<br />'.html_safe +
Chris@909 203 l(:text_git_repository_note)) +
Chris@441 204 content_tag('p', form.select(
Chris@441 205 :path_encoding, [nil] + Setting::ENCODINGS,
Chris@441 206 :label => l(:field_scm_path_encoding)
Chris@441 207 ) +
Chris@909 208 '<br />'.html_safe + l(:text_scm_path_encoding_note)) +
Chris@441 209 content_tag('p', form.check_box(
Chris@441 210 :extra_report_last_commit,
Chris@441 211 :label => l(:label_git_report_last_commit)
Chris@441 212 ))
Chris@0 213 end
Chris@0 214
Chris@0 215 def cvs_field_tags(form, repository)
Chris@441 216 content_tag('p', form.text_field(
Chris@441 217 :root_url,
Chris@441 218 :label => l(:field_cvsroot),
Chris@441 219 :size => 60, :required => true,
Chris@441 220 :disabled => !repository.new_record?)) +
Chris@441 221 content_tag('p', form.text_field(
Chris@441 222 :url,
Chris@441 223 :label => l(:field_cvs_module),
Chris@441 224 :size => 30, :required => true,
Chris@441 225 :disabled => !repository.new_record?)) +
Chris@441 226 content_tag('p', form.select(
Chris@441 227 :log_encoding, [nil] + Setting::ENCODINGS,
Chris@441 228 :label => l(:field_commit_logs_encoding), :required => true)) +
Chris@441 229 content_tag('p', form.select(
Chris@441 230 :path_encoding, [nil] + Setting::ENCODINGS,
Chris@441 231 :label => l(:field_scm_path_encoding)
Chris@441 232 ) +
Chris@909 233 '<br />'.html_safe + l(:text_scm_path_encoding_note))
Chris@0 234 end
Chris@0 235
Chris@0 236 def bazaar_field_tags(form, repository)
Chris@441 237 content_tag('p', form.text_field(
Chris@441 238 :url, :label => l(:field_path_to_repository),
Chris@441 239 :size => 60, :required => true,
Chris@441 240 :disabled => (repository && !repository.new_record?))) +
Chris@441 241 content_tag('p', form.select(
Chris@441 242 :log_encoding, [nil] + Setting::ENCODINGS,
Chris@441 243 :label => l(:field_commit_logs_encoding), :required => true))
Chris@0 244 end
Chris@245 245
Chris@0 246 def filesystem_field_tags(form, repository)
Chris@441 247 content_tag('p', form.text_field(
Chris@441 248 :url, :label => l(:field_root_directory),
Chris@245 249 :size => 60, :required => true,
Chris@245 250 :disabled => (repository && !repository.root_url.blank?))) +
Chris@245 251 content_tag('p', form.select(
Chris@245 252 :path_encoding, [nil] + Setting::ENCODINGS,
Chris@441 253 :label => l(:field_scm_path_encoding)
Chris@441 254 ) +
Chris@909 255 '<br />'.html_safe + l(:text_scm_path_encoding_note))
Chris@909 256 end
Chris@909 257
Chris@909 258 def index_commits(commits, heads, href_proc = nil)
Chris@909 259 return nil if commits.nil? or commits.first.parents.nil?
Chris@909 260 map = {}
Chris@909 261 commit_hashes = []
Chris@909 262 refs_map = {}
Chris@909 263 href_proc ||= Proc.new {|x|x}
Chris@909 264 heads.each{|r| refs_map[r.scmid] ||= []; refs_map[r.scmid] << r}
Chris@909 265 commits.reverse.each_with_index do |c, i|
Chris@909 266 h = {}
Chris@909 267 h[:parents] = c.parents.collect do |p|
Chris@909 268 [p.scmid, 0, 0]
Chris@909 269 end
Chris@909 270 h[:rdmid] = i
Chris@909 271 h[:space] = 0
Chris@909 272 h[:refs] = refs_map[c.scmid].join(" ") if refs_map.include? c.scmid
Chris@909 273 h[:scmid] = c.scmid
Chris@909 274 h[:href] = href_proc.call(c.scmid)
Chris@909 275 commit_hashes << h
Chris@909 276 map[c.scmid] = h
Chris@909 277 end
Chris@909 278 heads.sort! do |a,b|
Chris@909 279 a.to_s <=> b.to_s
Chris@909 280 end
Chris@909 281 j = 0
Chris@909 282 heads.each do |h|
Chris@909 283 if map.include? h.scmid then
Chris@909 284 j = mark_chain(j += 1, map[h.scmid], map)
Chris@909 285 end
Chris@909 286 end
Chris@909 287 # when no head matched anything use first commit
Chris@909 288 if j == 0 then
Chris@909 289 mark_chain(j += 1, map.values.first, map)
Chris@909 290 end
Chris@909 291 map
Chris@909 292 end
Chris@909 293
Chris@909 294 def mark_chain(mark, commit, map)
Chris@909 295 stack = [[mark, commit]]
Chris@909 296 markmax = mark
Chris@909 297 until stack.empty?
Chris@909 298 current = stack.pop
Chris@909 299 m, commit = current
Chris@909 300 commit[:space] = m if commit[:space] == 0
Chris@909 301 m1 = m - 1
Chris@909 302 commit[:parents].each_with_index do |p, i|
Chris@909 303 psha = p[0]
Chris@909 304 if map.include? psha and map[psha][:space] == 0 then
Chris@909 305 stack << [m1 += 1, map[psha]] if i == 0
Chris@909 306 stack = [[m1 += 1, map[psha]]] + stack if i > 0
Chris@909 307 end
Chris@909 308 end
Chris@909 309 markmax = m1 if markmax < m1
Chris@909 310 end
Chris@909 311 markmax
Chris@0 312 end
chris@741 313
chris@741 314 # Generates a link to a downloadable archive for a revision
chris@741 315 # Options:
chris@741 316 # * :text - Link text (default to the formatted revision)
chris@741 317 def link_to_revision_archive(repository, revision, project, options={})
chris@741 318 method = repository.class.name.demodulize.underscore + "_link_to_revision_archive"
chris@741 319 if repository.is_a?(Repository) &&
chris@741 320 respond_to?(method) && method != 'link_to_revision_archive'
chris@741 321 send(method, repository, revision, project, options)
chris@741 322 end
chris@741 323 end
chris@741 324
chris@741 325 def mercurial_link_to_revision_archive(repository, revision, project, options={})
chris@741 326 text = options.delete(:text) || format_revision(revision)
chris@741 327 rev = revision.respond_to?(:identifier) ? revision.identifier : revision
chris@741 328 if rev.blank? then rev = 'tip' end
chris@741 329 content_tag('a', h(text),
chris@741 330 { :href => "/hg/#{project.identifier}/archive/#{rev}.zip" }.merge(options));
chris@741 331 end
chris@741 332
Chris@0 333 end