annotate app/helpers/repositories_helper.rb @ 1327:287f201c2802 redmine-2.2-integration

Add italic
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Wed, 19 Jun 2013 20:56:22 +0100
parents bb32da3bea34
children 4f746d8966dd
rev   line source
Chris@909 1 # encoding: utf-8
Chris@909 2 #
Chris@441 3 # Redmine - project management software
Chris@1115 4 # Copyright (C) 2006-2012 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 module RepositoriesHelper
Chris@119 21 def format_revision(revision)
Chris@119 22 if revision.respond_to? :format_identifier
Chris@119 23 revision.format_identifier
Chris@119 24 else
Chris@119 25 revision.to_s
Chris@119 26 end
Chris@0 27 end
Chris@245 28
Chris@0 29 def truncate_at_line_break(text, length = 255)
Chris@0 30 if text
Chris@0 31 text.gsub(%r{^(.{#{length}}[^\n]*)\n.+$}m, '\\1...')
Chris@0 32 end
Chris@0 33 end
Chris@245 34
Chris@0 35 def render_properties(properties)
Chris@0 36 unless properties.nil? || properties.empty?
Chris@0 37 content = ''
Chris@0 38 properties.keys.sort.each do |property|
Chris@909 39 content << content_tag('li', "<b>#{h property}</b>: <span>#{h properties[property]}</span>".html_safe)
Chris@0 40 end
Chris@909 41 content_tag('ul', content.html_safe, :class => 'properties')
Chris@0 42 end
Chris@0 43 end
Chris@245 44
Chris@0 45 def render_changeset_changes
Chris@1115 46 changes = @changeset.filechanges.find(:all, :limit => 1000, :order => 'path').collect do |change|
Chris@0 47 case change.action
Chris@0 48 when 'A'
Chris@0 49 # Detects moved/copied files
Chris@0 50 if !change.from_path.blank?
Chris@441 51 change.action =
Chris@1115 52 @changeset.filechanges.detect {|c| c.action == 'D' && c.path == change.from_path} ? 'R' : 'C'
Chris@0 53 end
Chris@0 54 change
Chris@0 55 when 'D'
Chris@1115 56 @changeset.filechanges.detect {|c| c.from_path == change.path} ? nil : change
Chris@0 57 else
Chris@0 58 change
Chris@0 59 end
Chris@441 60 end.compact
Chris@441 61
Chris@0 62 tree = { }
Chris@0 63 changes.each do |change|
Chris@0 64 p = tree
Chris@0 65 dirs = change.path.to_s.split('/').select {|d| !d.blank?}
Chris@0 66 path = ''
Chris@0 67 dirs.each do |dir|
Chris@0 68 path += '/' + dir
Chris@0 69 p[:s] ||= {}
Chris@0 70 p = p[:s]
Chris@0 71 p[path] ||= {}
Chris@0 72 p = p[path]
Chris@0 73 end
Chris@0 74 p[:c] = change
Chris@0 75 end
Chris@0 76 render_changes_tree(tree[:s])
Chris@0 77 end
Chris@245 78
Chris@0 79 def render_changes_tree(tree)
Chris@0 80 return '' if tree.nil?
Chris@0 81 output = ''
Chris@0 82 output << '<ul>'
Chris@0 83 tree.keys.sort.each do |file|
Chris@0 84 style = 'change'
Chris@0 85 text = File.basename(h(file))
Chris@0 86 if s = tree[file][:s]
Chris@0 87 style << ' folder'
Chris@0 88 path_param = to_path_param(@repository.relative_path(file))
Chris@909 89 text = link_to(h(text), :controller => 'repositories',
Chris@0 90 :action => 'show',
Chris@0 91 :id => @project,
Chris@1115 92 :repository_id => @repository.identifier_param,
Chris@0 93 :path => path_param,
Chris@119 94 :rev => @changeset.identifier)
Chris@1115 95 output << "<li class='#{style}'>#{text}"
Chris@0 96 output << render_changes_tree(s)
Chris@1115 97 output << "</li>"
Chris@0 98 elsif c = tree[file][:c]
Chris@0 99 style << " change-#{c.action}"
Chris@0 100 path_param = to_path_param(@repository.relative_path(c.path))
Chris@909 101 text = link_to(h(text), :controller => 'repositories',
Chris@0 102 :action => 'entry',
Chris@0 103 :id => @project,
Chris@1115 104 :repository_id => @repository.identifier_param,
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@1115 111 :repository_id => @repository.identifier_param,
Chris@0 112 :path => path_param,
Chris@909 113 :rev => @changeset.identifier) + ') '.html_safe if c.action == 'M'
Chris@909 114 text << ' '.html_safe + content_tag('span', h(c.from_path), :class => 'copied-from') unless c.from_path.blank?
Chris@0 115 output << "<li class='#{style}'>#{text}</li>"
Chris@0 116 end
Chris@0 117 end
Chris@0 118 output << '</ul>'
Chris@909 119 output.html_safe
Chris@0 120 end
Chris@245 121
Chris@245 122 def repository_field_tags(form, repository)
Chris@0 123 method = repository.class.name.demodulize.underscore + "_field_tags"
Chris@245 124 if repository.is_a?(Repository) &&
Chris@245 125 respond_to?(method) && method != 'repository_field_tags'
Chris@245 126 send(method, form, repository)
Chris@245 127 end
Chris@0 128 end
Chris@245 129
Chris@0 130 def scm_select_tag(repository)
Chris@0 131 scm_options = [["--- #{l(:actionview_instancetag_blank_option)} ---", '']]
Chris@0 132 Redmine::Scm::Base.all.each do |scm|
Chris@245 133 if Setting.enabled_scm.include?(scm) ||
Chris@245 134 (repository && repository.class.name.demodulize == scm)
Chris@245 135 scm_options << ["Repository::#{scm}".constantize.scm_name, scm]
Chris@245 136 end
Chris@0 137 end
Chris@441 138 select_tag('repository_scm',
Chris@0 139 options_for_select(scm_options, repository.class.name.demodulize),
Chris@0 140 :disabled => (repository && !repository.new_record?),
Chris@1115 141 :data => {:remote => true, :method => 'get'})
Chris@0 142 end
Chris@245 143
Chris@0 144 def with_leading_slash(path)
Chris@0 145 path.to_s.starts_with?('/') ? path : "/#{path}"
Chris@0 146 end
Chris@245 147
Chris@0 148 def without_leading_slash(path)
Chris@0 149 path.gsub(%r{^/+}, '')
Chris@0 150 end
Chris@0 151
Chris@0 152 def subversion_field_tags(form, repository)
Chris@245 153 content_tag('p', form.text_field(:url, :size => 60, :required => true,
Chris@1115 154 :disabled => !repository.safe_attribute?('url')) +
Chris@909 155 '<br />'.html_safe +
Chris@909 156 '(file:///, http://, https://, svn://, svn+[tunnelscheme]://)') +
Chris@0 157 content_tag('p', form.text_field(:login, :size => 30)) +
Chris@245 158 content_tag('p', form.password_field(
Chris@245 159 :password, :size => 30, :name => 'ignore',
Chris@245 160 :value => ((repository.new_record? || repository.password.blank?) ? '' : ('x'*15)),
Chris@245 161 :onfocus => "this.value=''; this.name='repository[password]';",
Chris@245 162 :onchange => "this.name='repository[password]';"))
Chris@0 163 end
Chris@0 164
Chris@0 165 def darcs_field_tags(form, repository)
Chris@441 166 content_tag('p', form.text_field(
Chris@441 167 :url, :label => l(:field_path_to_repository),
Chris@441 168 :size => 60, :required => true,
Chris@1115 169 :disabled => !repository.safe_attribute?('url'))) +
Chris@441 170 content_tag('p', form.select(
Chris@441 171 :log_encoding, [nil] + Setting::ENCODINGS,
Chris@441 172 :label => l(:field_commit_logs_encoding), :required => true))
Chris@0 173 end
Chris@245 174
Chris@0 175 def mercurial_field_tags(form, repository)
Chris@441 176 content_tag('p', form.text_field(
Chris@441 177 :url, :label => l(:field_path_to_repository),
Chris@245 178 :size => 60, :required => true,
Chris@1115 179 :disabled => !repository.safe_attribute?('url')
Chris@441 180 ) +
Chris@909 181 '<br />'.html_safe + l(:text_mercurial_repository_note)) +
Chris@441 182 content_tag('p', form.select(
Chris@441 183 :path_encoding, [nil] + Setting::ENCODINGS,
Chris@441 184 :label => l(:field_scm_path_encoding)
Chris@441 185 ) +
Chris@909 186 '<br />'.html_safe + l(:text_scm_path_encoding_note))
Chris@0 187 end
Chris@0 188
Chris@0 189 def git_field_tags(form, repository)
Chris@441 190 content_tag('p', form.text_field(
Chris@441 191 :url, :label => l(:field_path_to_repository),
Chris@245 192 :size => 60, :required => true,
Chris@1115 193 :disabled => !repository.safe_attribute?('url')
Chris@441 194 ) +
Chris@909 195 '<br />'.html_safe +
Chris@909 196 l(:text_git_repository_note)) +
Chris@441 197 content_tag('p', form.select(
Chris@441 198 :path_encoding, [nil] + Setting::ENCODINGS,
Chris@441 199 :label => l(:field_scm_path_encoding)
Chris@441 200 ) +
Chris@909 201 '<br />'.html_safe + l(:text_scm_path_encoding_note)) +
Chris@441 202 content_tag('p', form.check_box(
Chris@441 203 :extra_report_last_commit,
Chris@441 204 :label => l(:label_git_report_last_commit)
Chris@441 205 ))
Chris@0 206 end
Chris@0 207
Chris@0 208 def cvs_field_tags(form, repository)
Chris@441 209 content_tag('p', form.text_field(
Chris@441 210 :root_url,
Chris@441 211 :label => l(:field_cvsroot),
Chris@441 212 :size => 60, :required => true,
Chris@1115 213 :disabled => !repository.safe_attribute?('root_url'))) +
Chris@441 214 content_tag('p', form.text_field(
Chris@441 215 :url,
Chris@441 216 :label => l(:field_cvs_module),
Chris@441 217 :size => 30, :required => true,
Chris@1115 218 :disabled => !repository.safe_attribute?('url'))) +
Chris@441 219 content_tag('p', form.select(
Chris@441 220 :log_encoding, [nil] + Setting::ENCODINGS,
Chris@441 221 :label => l(:field_commit_logs_encoding), :required => true)) +
Chris@441 222 content_tag('p', form.select(
Chris@441 223 :path_encoding, [nil] + Setting::ENCODINGS,
Chris@441 224 :label => l(:field_scm_path_encoding)
Chris@441 225 ) +
Chris@909 226 '<br />'.html_safe + l(:text_scm_path_encoding_note))
Chris@0 227 end
Chris@0 228
Chris@0 229 def bazaar_field_tags(form, repository)
Chris@441 230 content_tag('p', form.text_field(
Chris@441 231 :url, :label => l(:field_path_to_repository),
Chris@441 232 :size => 60, :required => true,
Chris@1115 233 :disabled => !repository.safe_attribute?('url'))) +
Chris@441 234 content_tag('p', form.select(
Chris@441 235 :log_encoding, [nil] + Setting::ENCODINGS,
Chris@441 236 :label => l(:field_commit_logs_encoding), :required => true))
Chris@0 237 end
Chris@245 238
Chris@0 239 def filesystem_field_tags(form, repository)
Chris@441 240 content_tag('p', form.text_field(
Chris@441 241 :url, :label => l(:field_root_directory),
Chris@245 242 :size => 60, :required => true,
Chris@1115 243 :disabled => !repository.safe_attribute?('url'))) +
Chris@245 244 content_tag('p', form.select(
Chris@245 245 :path_encoding, [nil] + Setting::ENCODINGS,
Chris@441 246 :label => l(:field_scm_path_encoding)
Chris@441 247 ) +
Chris@909 248 '<br />'.html_safe + l(:text_scm_path_encoding_note))
Chris@909 249 end
Chris@909 250
Chris@1115 251 def index_commits(commits, heads)
Chris@909 252 return nil if commits.nil? or commits.first.parents.nil?
Chris@909 253 refs_map = {}
Chris@1115 254 heads.each do |head|
Chris@1115 255 refs_map[head.scmid] ||= []
Chris@1115 256 refs_map[head.scmid] << head
Chris@909 257 end
Chris@1115 258 commits_by_scmid = {}
Chris@1115 259 commits.reverse.each_with_index do |commit, commit_index|
Chris@1115 260 commits_by_scmid[commit.scmid] = {
Chris@1115 261 :parent_scmids => commit.parents.collect { |parent| parent.scmid },
Chris@1115 262 :rdmid => commit_index,
Chris@1115 263 :refs => refs_map.include?(commit.scmid) ? refs_map[commit.scmid].join(" ") : nil,
Chris@1115 264 :scmid => commit.scmid,
Chris@1115 265 :href => block_given? ? yield(commit.scmid) : commit.scmid
Chris@1115 266 }
Chris@909 267 end
Chris@1115 268 heads.sort! { |head1, head2| head1.to_s <=> head2.to_s }
Chris@1115 269 space = nil
Chris@1115 270 heads.each do |head|
Chris@1115 271 if commits_by_scmid.include? head.scmid
Chris@1115 272 space = index_head((space || -1) + 1, head, commits_by_scmid)
Chris@909 273 end
Chris@909 274 end
Chris@909 275 # when no head matched anything use first commit
Chris@1115 276 space ||= index_head(0, commits.first, commits_by_scmid)
Chris@1115 277 return commits_by_scmid, space
Chris@909 278 end
Chris@909 279
Chris@1115 280 def index_head(space, commit, commits_by_scmid)
Chris@1115 281 stack = [[space, commits_by_scmid[commit.scmid]]]
Chris@1115 282 max_space = space
Chris@909 283 until stack.empty?
Chris@1115 284 space, commit = stack.pop
Chris@1115 285 commit[:space] = space if commit[:space].nil?
Chris@1115 286 space -= 1
Chris@1115 287 commit[:parent_scmids].each_with_index do |parent_scmid, parent_index|
Chris@1115 288 parent_commit = commits_by_scmid[parent_scmid]
Chris@1115 289 if parent_commit and parent_commit[:space].nil?
Chris@1115 290 stack.unshift [space += 1, parent_commit]
Chris@909 291 end
Chris@909 292 end
Chris@1115 293 max_space = space if max_space < space
Chris@909 294 end
Chris@1115 295 max_space
Chris@0 296 end
chris@741 297
chris@741 298 # Generates a link to a downloadable archive for a revision
chris@741 299 # Options:
chris@741 300 # * :text - Link text (default to the formatted revision)
chris@741 301 def link_to_revision_archive(repository, revision, project, options={})
chris@741 302 method = repository.class.name.demodulize.underscore + "_link_to_revision_archive"
chris@741 303 if repository.is_a?(Repository) &&
chris@741 304 respond_to?(method) && method != 'link_to_revision_archive'
chris@741 305 send(method, repository, revision, project, options)
chris@741 306 end
chris@741 307 end
chris@741 308
chris@741 309 def mercurial_link_to_revision_archive(repository, revision, project, options={})
chris@741 310 text = options.delete(:text) || format_revision(revision)
chris@741 311 rev = revision.respond_to?(:identifier) ? revision.identifier : revision
chris@741 312 if rev.blank? then rev = 'tip' end
chris@741 313 content_tag('a', h(text),
chris@741 314 { :href => "/hg/#{project.identifier}/archive/#{rev}.zip" }.merge(options));
chris@741 315 end
chris@741 316
Chris@0 317 end