Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

repositories_controller.rb 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2016 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. require 'SVG/Graph/Bar'
  18. require 'SVG/Graph/BarHorizontal'
  19. require 'digest/sha1'
  20. require 'redmine/scm/adapters'
  21. class ChangesetNotFound < Exception; end
  22. class InvalidRevisionParam < Exception; end
  23. class RepositoriesController < ApplicationController
  24. menu_item :repository
  25. menu_item :settings, :only => [:new, :create, :edit, :update, :destroy, :committers]
  26. default_search_scope :changesets
  27. before_action :find_project_by_project_id, :only => [:new, :create]
  28. before_action :build_new_repository_from_params, :only => [:new, :create]
  29. before_action :find_repository, :only => [:edit, :update, :destroy, :committers]
  30. before_action :find_project_repository, :except => [:new, :create, :edit, :update, :destroy, :committers]
  31. before_action :find_changeset, :only => [:revision, :add_related_issue, :remove_related_issue]
  32. before_action :authorize
  33. accept_rss_auth :revisions
  34. rescue_from Redmine::Scm::Adapters::CommandFailed, :with => :show_error_command_failed
  35. def new
  36. @repository.is_default = @project.repository.nil?
  37. end
  38. def create
  39. if @repository.save
  40. redirect_to settings_project_path(@project, :tab => 'repositories')
  41. else
  42. render :action => 'new'
  43. end
  44. end
  45. def edit
  46. end
  47. def update
  48. @repository.safe_attributes = params[:repository]
  49. if @repository.save
  50. redirect_to settings_project_path(@project, :tab => 'repositories')
  51. else
  52. render :action => 'edit'
  53. end
  54. end
  55. def committers
  56. @committers = @repository.committers
  57. @users = @project.users.to_a
  58. additional_user_ids = @committers.collect(&:last).collect(&:to_i) - @users.collect(&:id)
  59. @users += User.where(:id => additional_user_ids).to_a unless additional_user_ids.empty?
  60. @users.compact!
  61. @users.sort!
  62. if request.post? && params[:committers].is_a?(Hash)
  63. # Build a hash with repository usernames as keys and corresponding user ids as values
  64. @repository.committer_ids = params[:committers].values.inject({}) {|h, c| h[c.first] = c.last; h}
  65. flash[:notice] = l(:notice_successful_update)
  66. redirect_to settings_project_path(@project, :tab => 'repositories')
  67. end
  68. end
  69. def destroy
  70. @repository.destroy if request.delete?
  71. redirect_to settings_project_path(@project, :tab => 'repositories')
  72. end
  73. def show
  74. @repository.fetch_changesets if @project.active? && Setting.autofetch_changesets? && @path.empty?
  75. @entries = @repository.entries(@path, @rev)
  76. @changeset = @repository.find_changeset_by_name(@rev)
  77. if request.xhr?
  78. @entries ? render(:partial => 'dir_list_content') : head(200)
  79. else
  80. (show_error_not_found; return) unless @entries
  81. @changesets = @repository.latest_changesets(@path, @rev)
  82. @properties = @repository.properties(@path, @rev)
  83. @repositories = @project.repositories
  84. render :action => 'show'
  85. end
  86. end
  87. alias_method :browse, :show
  88. def changes
  89. @entry = @repository.entry(@path, @rev)
  90. (show_error_not_found; return) unless @entry
  91. @changesets = @repository.latest_changesets(@path, @rev, Setting.repository_log_display_limit.to_i)
  92. @properties = @repository.properties(@path, @rev)
  93. @changeset = @repository.find_changeset_by_name(@rev)
  94. end
  95. def revisions
  96. @changeset_count = @repository.changesets.count
  97. @changeset_pages = Paginator.new @changeset_count,
  98. per_page_option,
  99. params['page']
  100. @changesets = @repository.changesets.
  101. limit(@changeset_pages.per_page).
  102. offset(@changeset_pages.offset).
  103. includes(:user, :repository, :parents).
  104. to_a
  105. respond_to do |format|
  106. format.html { render :layout => false if request.xhr? }
  107. format.atom { render_feed(@changesets, :title => "#{@project.name}: #{l(:label_revision_plural)}") }
  108. end
  109. end
  110. def raw
  111. entry_and_raw(true)
  112. end
  113. def entry
  114. entry_and_raw(false)
  115. end
  116. def entry_and_raw(is_raw)
  117. @entry = @repository.entry(@path, @rev)
  118. (show_error_not_found; return) unless @entry
  119. # If the entry is a dir, show the browser
  120. (show; return) if @entry.is_dir?
  121. if is_raw
  122. # Force the download
  123. send_opt = { :filename => filename_for_content_disposition(@path.split('/').last) }
  124. send_type = Redmine::MimeType.of(@path)
  125. send_opt[:type] = send_type.to_s if send_type
  126. send_opt[:disposition] = disposition(@path)
  127. send_data @repository.cat(@path, @rev), send_opt
  128. else
  129. if !@entry.size || @entry.size <= Setting.file_max_size_displayed.to_i.kilobyte
  130. content = @repository.cat(@path, @rev)
  131. (show_error_not_found; return) unless content
  132. if content.size <= Setting.file_max_size_displayed.to_i.kilobyte &&
  133. is_entry_text_data?(content, @path)
  134. # TODO: UTF-16
  135. # Prevent empty lines when displaying a file with Windows style eol
  136. # Is this needed? AttachmentsController simply reads file.
  137. @content = content.gsub("\r\n", "\n")
  138. end
  139. end
  140. @changeset = @repository.find_changeset_by_name(@rev)
  141. end
  142. end
  143. private :entry_and_raw
  144. def is_entry_text_data?(ent, path)
  145. # UTF-16 contains "\x00".
  146. # It is very strict that file contains less than 30% of ascii symbols
  147. # in non Western Europe.
  148. return true if Redmine::MimeType.is_type?('text', path)
  149. # Ruby 1.8.6 has a bug of integer divisions.
  150. # http://apidock.com/ruby/v1_8_6_287/String/is_binary_data%3F
  151. return false if ent.is_binary_data?
  152. true
  153. end
  154. private :is_entry_text_data?
  155. def annotate
  156. @entry = @repository.entry(@path, @rev)
  157. (show_error_not_found; return) unless @entry
  158. @annotate = @repository.scm.annotate(@path, @rev)
  159. if @annotate.nil? || @annotate.empty?
  160. @annotate = nil
  161. @error_message = l(:error_scm_annotate)
  162. else
  163. ann_buf_size = 0
  164. @annotate.lines.each do |buf|
  165. ann_buf_size += buf.size
  166. end
  167. if ann_buf_size > Setting.file_max_size_displayed.to_i.kilobyte
  168. @annotate = nil
  169. @error_message = l(:error_scm_annotate_big_text_file)
  170. end
  171. end
  172. @changeset = @repository.find_changeset_by_name(@rev)
  173. end
  174. def revision
  175. respond_to do |format|
  176. format.html
  177. format.js {render :layout => false}
  178. end
  179. end
  180. # Adds a related issue to a changeset
  181. # POST /projects/:project_id/repository/(:repository_id/)revisions/:rev/issues
  182. def add_related_issue
  183. issue_id = params[:issue_id].to_s.sub(/^#/,'')
  184. @issue = @changeset.find_referenced_issue_by_id(issue_id)
  185. if @issue && (!@issue.visible? || @changeset.issues.include?(@issue))
  186. @issue = nil
  187. end
  188. if @issue
  189. @changeset.issues << @issue
  190. end
  191. end
  192. # Removes a related issue from a changeset
  193. # DELETE /projects/:project_id/repository/(:repository_id/)revisions/:rev/issues/:issue_id
  194. def remove_related_issue
  195. @issue = Issue.visible.find_by_id(params[:issue_id])
  196. if @issue
  197. @changeset.issues.delete(@issue)
  198. end
  199. end
  200. def diff
  201. if params[:format] == 'diff'
  202. @diff = @repository.diff(@path, @rev, @rev_to)
  203. (show_error_not_found; return) unless @diff
  204. filename = "changeset_r#{@rev}"
  205. filename << "_r#{@rev_to}" if @rev_to
  206. send_data @diff.join, :filename => "#{filename}.diff",
  207. :type => 'text/x-patch',
  208. :disposition => 'attachment'
  209. else
  210. @diff_type = params[:type] || User.current.pref[:diff_type] || 'inline'
  211. @diff_type = 'inline' unless %w(inline sbs).include?(@diff_type)
  212. # Save diff type as user preference
  213. if User.current.logged? && @diff_type != User.current.pref[:diff_type]
  214. User.current.pref[:diff_type] = @diff_type
  215. User.current.preference.save
  216. end
  217. @cache_key = "repositories/diff/#{@repository.id}/" +
  218. Digest::MD5.hexdigest("#{@path}-#{@rev}-#{@rev_to}-#{@diff_type}-#{current_language}")
  219. unless read_fragment(@cache_key)
  220. @diff = @repository.diff(@path, @rev, @rev_to)
  221. show_error_not_found unless @diff
  222. end
  223. @changeset = @repository.find_changeset_by_name(@rev)
  224. @changeset_to = @rev_to ? @repository.find_changeset_by_name(@rev_to) : nil
  225. @diff_format_revisions = @repository.diff_format_revisions(@changeset, @changeset_to)
  226. end
  227. end
  228. def stats
  229. end
  230. def graph
  231. data = nil
  232. case params[:graph]
  233. when "commits_per_month"
  234. data = graph_commits_per_month(@repository)
  235. when "commits_per_author"
  236. data = graph_commits_per_author(@repository)
  237. end
  238. if data
  239. headers["Content-Type"] = "image/svg+xml"
  240. send_data(data, :type => "image/svg+xml", :disposition => "inline")
  241. else
  242. render_404
  243. end
  244. end
  245. private
  246. def build_new_repository_from_params
  247. scm = params[:repository_scm] || (Redmine::Scm::Base.all & Setting.enabled_scm).first
  248. unless @repository = Repository.factory(scm)
  249. render_404
  250. return
  251. end
  252. @repository.project = @project
  253. @repository.safe_attributes = params[:repository]
  254. @repository
  255. end
  256. def find_repository
  257. @repository = Repository.find(params[:id])
  258. @project = @repository.project
  259. rescue ActiveRecord::RecordNotFound
  260. render_404
  261. end
  262. REV_PARAM_RE = %r{\A[a-f0-9]*\Z}i
  263. def find_project_repository
  264. @project = Project.find(params[:id])
  265. if params[:repository_id].present?
  266. @repository = @project.repositories.find_by_identifier_param(params[:repository_id])
  267. else
  268. @repository = @project.repository
  269. end
  270. (render_404; return false) unless @repository
  271. @path = params[:path].is_a?(Array) ? params[:path].join('/') : params[:path].to_s
  272. @rev = params[:rev].blank? ? @repository.default_branch : params[:rev].to_s.strip
  273. @rev_to = params[:rev_to]
  274. unless @rev.to_s.match(REV_PARAM_RE) && @rev_to.to_s.match(REV_PARAM_RE)
  275. if @repository.branches.blank?
  276. raise InvalidRevisionParam
  277. end
  278. end
  279. rescue ActiveRecord::RecordNotFound
  280. render_404
  281. rescue InvalidRevisionParam
  282. show_error_not_found
  283. end
  284. def find_changeset
  285. if @rev.present?
  286. @changeset = @repository.find_changeset_by_name(@rev)
  287. end
  288. show_error_not_found unless @changeset
  289. end
  290. def show_error_not_found
  291. render_error :message => l(:error_scm_not_found), :status => 404
  292. end
  293. # Handler for Redmine::Scm::Adapters::CommandFailed exception
  294. def show_error_command_failed(exception)
  295. render_error l(:error_scm_command_failed, exception.message)
  296. end
  297. def graph_commits_per_month(repository)
  298. @date_to = User.current.today
  299. @date_from = @date_to << 11
  300. @date_from = Date.civil(@date_from.year, @date_from.month, 1)
  301. commits_by_day = Changeset.
  302. where("repository_id = ? AND commit_date BETWEEN ? AND ?", repository.id, @date_from, @date_to).
  303. group(:commit_date).
  304. count
  305. commits_by_month = [0] * 12
  306. commits_by_day.each {|c| commits_by_month[(@date_to.month - c.first.to_date.month) % 12] += c.last }
  307. changes_by_day = Change.
  308. joins(:changeset).
  309. where("#{Changeset.table_name}.repository_id = ? AND #{Changeset.table_name}.commit_date BETWEEN ? AND ?", repository.id, @date_from, @date_to).
  310. group(:commit_date).
  311. count
  312. changes_by_month = [0] * 12
  313. changes_by_day.each {|c| changes_by_month[(@date_to.month - c.first.to_date.month) % 12] += c.last }
  314. fields = []
  315. today = User.current.today
  316. 12.times {|m| fields << month_name(((today.month - 1 - m) % 12) + 1)}
  317. graph = SVG::Graph::Bar.new(
  318. :height => 300,
  319. :width => 800,
  320. :fields => fields.reverse,
  321. :stack => :side,
  322. :scale_integers => true,
  323. :step_x_labels => 2,
  324. :show_data_values => false,
  325. :graph_title => l(:label_commits_per_month),
  326. :show_graph_title => true
  327. )
  328. graph.add_data(
  329. :data => commits_by_month[0..11].reverse,
  330. :title => l(:label_revision_plural)
  331. )
  332. graph.add_data(
  333. :data => changes_by_month[0..11].reverse,
  334. :title => l(:label_change_plural)
  335. )
  336. graph.burn
  337. end
  338. def graph_commits_per_author(repository)
  339. #data
  340. stats = repository.stats_by_author
  341. fields, commits_data, changes_data = [], [], []
  342. stats.each do |name, hsh|
  343. fields << name
  344. commits_data << hsh[:commits_count]
  345. changes_data << hsh[:changes_count]
  346. end
  347. #expand to 10 values if needed
  348. fields = fields + [""]*(10 - fields.length) if fields.length<10
  349. commits_data = commits_data + [0]*(10 - commits_data.length) if commits_data.length<10
  350. changes_data = changes_data + [0]*(10 - changes_data.length) if changes_data.length<10
  351. # Remove email address in usernames
  352. fields = fields.collect {|c| c.gsub(%r{<.+@.+>}, '') }
  353. #prepare graph
  354. graph = SVG::Graph::BarHorizontal.new(
  355. :height => 30 * commits_data.length,
  356. :width => 800,
  357. :fields => fields,
  358. :stack => :side,
  359. :scale_integers => true,
  360. :show_data_values => false,
  361. :rotate_y_labels => false,
  362. :graph_title => l(:label_commits_per_author),
  363. :show_graph_title => true
  364. )
  365. graph.add_data(
  366. :data => commits_data,
  367. :title => l(:label_revision_plural)
  368. )
  369. graph.add_data(
  370. :data => changes_data,
  371. :title => l(:label_change_plural)
  372. )
  373. graph.burn
  374. end
  375. def disposition(path)
  376. if Redmine::MimeType.of(@path) == "application/pdf"
  377. 'inline'
  378. else
  379. 'attachment'
  380. end
  381. end
  382. end