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_controller.rb 14KB

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