annotate .svn/pristine/af/af6039d5edc137b7d619583ed0d31102abfb5953.svn-base @ 1613:90bed4e10cc8 deploy

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