annotate .svn/pristine/ae/ae9b39171caeb6f8f6a13fdb73ccbb11ad433254.svn-base @ 1295:622f24f53b42 redmine-2.3

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