PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/app/helpers/repositories_helper.rb

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