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 13KB

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