You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

repositories_helper.rb 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. # encoding: utf-8
  2. #
  3. # Redmine - project management software
  4. # Copyright (C) 2006-2011 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.changes.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.changes.detect {|c| c.action == 'D' && c.path == change.from_path} ? 'R' : 'C'
  51. end
  52. change
  53. when 'D'
  54. @changeset.changes.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. :onchange => remote_function(
  136. :url => new_project_repository_path(@project),
  137. :method => :get,
  138. :update => 'content',
  139. :with => "Form.serialize(this.form)")
  140. )
  141. end
  142. def with_leading_slash(path)
  143. path.to_s.starts_with?('/') ? path : "/#{path}"
  144. end
  145. def without_leading_slash(path)
  146. path.gsub(%r{^/+}, '')
  147. end
  148. def subversion_field_tags(form, repository)
  149. content_tag('p', form.text_field(:url, :size => 60, :required => true,
  150. :disabled => (repository && !repository.root_url.blank?)) +
  151. '<br />'.html_safe +
  152. '(file:///, http://, https://, svn://, svn+[tunnelscheme]://)') +
  153. content_tag('p', form.text_field(:login, :size => 30)) +
  154. content_tag('p', form.password_field(
  155. :password, :size => 30, :name => 'ignore',
  156. :value => ((repository.new_record? || repository.password.blank?) ? '' : ('x'*15)),
  157. :onfocus => "this.value=''; this.name='repository[password]';",
  158. :onchange => "this.name='repository[password]';"))
  159. end
  160. def darcs_field_tags(form, repository)
  161. content_tag('p', form.text_field(
  162. :url, :label => l(:field_path_to_repository),
  163. :size => 60, :required => true,
  164. :disabled => (repository && !repository.new_record?))) +
  165. content_tag('p', form.select(
  166. :log_encoding, [nil] + Setting::ENCODINGS,
  167. :label => l(:field_commit_logs_encoding), :required => true))
  168. end
  169. def mercurial_field_tags(form, repository)
  170. content_tag('p', form.text_field(
  171. :url, :label => l(:field_path_to_repository),
  172. :size => 60, :required => true,
  173. :disabled => (repository && !repository.root_url.blank?)
  174. ) +
  175. '<br />'.html_safe + l(:text_mercurial_repository_note)) +
  176. content_tag('p', form.select(
  177. :path_encoding, [nil] + Setting::ENCODINGS,
  178. :label => l(:field_scm_path_encoding)
  179. ) +
  180. '<br />'.html_safe + l(:text_scm_path_encoding_note))
  181. end
  182. def git_field_tags(form, repository)
  183. content_tag('p', form.text_field(
  184. :url, :label => l(:field_path_to_repository),
  185. :size => 60, :required => true,
  186. :disabled => (repository && !repository.root_url.blank?)
  187. ) +
  188. '<br />'.html_safe +
  189. l(:text_git_repository_note)) +
  190. content_tag('p', form.select(
  191. :path_encoding, [nil] + Setting::ENCODINGS,
  192. :label => l(:field_scm_path_encoding)
  193. ) +
  194. '<br />'.html_safe + l(:text_scm_path_encoding_note)) +
  195. content_tag('p', form.check_box(
  196. :extra_report_last_commit,
  197. :label => l(:label_git_report_last_commit)
  198. ))
  199. end
  200. def cvs_field_tags(form, repository)
  201. content_tag('p', form.text_field(
  202. :root_url,
  203. :label => l(:field_cvsroot),
  204. :size => 60, :required => true,
  205. :disabled => !repository.new_record?)) +
  206. content_tag('p', form.text_field(
  207. :url,
  208. :label => l(:field_cvs_module),
  209. :size => 30, :required => true,
  210. :disabled => !repository.new_record?)) +
  211. content_tag('p', form.select(
  212. :log_encoding, [nil] + Setting::ENCODINGS,
  213. :label => l(:field_commit_logs_encoding), :required => true)) +
  214. content_tag('p', form.select(
  215. :path_encoding, [nil] + Setting::ENCODINGS,
  216. :label => l(:field_scm_path_encoding)
  217. ) +
  218. '<br />'.html_safe + l(:text_scm_path_encoding_note))
  219. end
  220. def bazaar_field_tags(form, repository)
  221. content_tag('p', form.text_field(
  222. :url, :label => l(:field_path_to_repository),
  223. :size => 60, :required => true,
  224. :disabled => (repository && !repository.new_record?))) +
  225. content_tag('p', form.select(
  226. :log_encoding, [nil] + Setting::ENCODINGS,
  227. :label => l(:field_commit_logs_encoding), :required => true))
  228. end
  229. def filesystem_field_tags(form, repository)
  230. content_tag('p', form.text_field(
  231. :url, :label => l(:field_root_directory),
  232. :size => 60, :required => true,
  233. :disabled => (repository && !repository.root_url.blank?))) +
  234. content_tag('p', form.select(
  235. :path_encoding, [nil] + Setting::ENCODINGS,
  236. :label => l(:field_scm_path_encoding)
  237. ) +
  238. '<br />'.html_safe + l(:text_scm_path_encoding_note))
  239. end
  240. def index_commits(commits, heads)
  241. return nil if commits.nil? or commits.first.parents.nil?
  242. refs_map = {}
  243. heads.each do |head|
  244. refs_map[head.scmid] ||= []
  245. refs_map[head.scmid] << head
  246. end
  247. commits_by_scmid = {}
  248. commits.reverse.each_with_index do |commit, commit_index|
  249. commits_by_scmid[commit.scmid] = {
  250. :parent_scmids => commit.parents.collect { |parent| parent.scmid },
  251. :rdmid => commit_index,
  252. :refs => refs_map.include?(commit.scmid) ? refs_map[commit.scmid].join(" ") : nil,
  253. :scmid => commit.scmid,
  254. :href => block_given? ? yield(commit.scmid) : commit.scmid
  255. }
  256. end
  257. heads.sort! { |head1, head2| head1.to_s <=> head2.to_s }
  258. space = nil
  259. heads.each do |head|
  260. if commits_by_scmid.include? head.scmid
  261. space = index_head((space || -1) + 1, head, commits_by_scmid)
  262. end
  263. end
  264. # when no head matched anything use first commit
  265. space ||= index_head(0, commits.first, commits_by_scmid)
  266. return commits_by_scmid, space
  267. end
  268. def index_head(space, commit, commits_by_scmid)
  269. stack = [[space, commits_by_scmid[commit.scmid]]]
  270. max_space = space
  271. until stack.empty?
  272. space, commit = stack.pop
  273. commit[:space] = space if commit[:space].nil?
  274. space -= 1
  275. commit[:parent_scmids].each_with_index do |parent_scmid, parent_index|
  276. parent_commit = commits_by_scmid[parent_scmid]
  277. if parent_commit and parent_commit[:space].nil?
  278. stack.unshift [space += 1, parent_commit]
  279. end
  280. end
  281. max_space = space if max_space < space
  282. end
  283. max_space
  284. end
  285. end