repositories_controller.rb
434 lines
| 14.5 KiB
| text/x-ruby
|
RubyLexer
|
r2738 | # Redmine - project management software | ||
|
r8420 | # Copyright (C) 2006-2012 Jean-Philippe Lang | ||
|
r103 | # | ||
# This program is free software; you can redistribute it and/or | ||||
# modify it under the terms of the GNU General Public License | ||||
# as published by the Free Software Foundation; either version 2 | ||||
# of the License, or (at your option) any later version. | ||||
|
r5754 | # | ||
|
r103 | # This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
# GNU General Public License for more details. | ||||
|
r5754 | # | ||
|
r103 | # You should have received a copy of the GNU General Public License | ||
# along with this program; if not, write to the Free Software | ||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
|
r377 | require 'SVG/Graph/Bar' | ||
require 'SVG/Graph/BarHorizontal' | ||||
|
r496 | require 'digest/sha1' | ||
|
r9346 | require 'redmine/scm/adapters/abstract_adapter' | ||
|
r377 | |||
|
r1309 | class ChangesetNotFound < Exception; end | ||
class InvalidRevisionParam < Exception; end | ||||
|
r925 | |||
|
r103 | class RepositoriesController < ApplicationController | ||
|
r1062 | menu_item :repository | ||
|
r8528 | menu_item :settings, :only => [:new, :create, :edit, :update, :destroy, :committers] | ||
|
r2829 | default_search_scope :changesets | ||
|
r5497 | |||
|
r8528 | before_filter :find_project_by_project_id, :only => [:new, :create] | ||
before_filter :find_repository, :only => [:edit, :update, :destroy, :committers] | ||||
before_filter :find_project_repository, :except => [:new, :create, :edit, :update, :destroy, :committers] | ||||
|
r8657 | before_filter :find_changeset, :only => [:revision, :add_related_issue, :remove_related_issue] | ||
|
r714 | before_filter :authorize | ||
|
r6077 | accept_rss_auth :revisions | ||
|
r5497 | |||
|
r1541 | rescue_from Redmine::Scm::Adapters::CommandFailed, :with => :show_error_command_failed | ||
|
r5497 | |||
|
r8528 | def new | ||
|
r8533 | scm = params[:repository_scm] || (Redmine::Scm::Base.all & Setting.enabled_scm).first | ||
|
r8528 | @repository = Repository.factory(scm) | ||
|
r8530 | @repository.is_default = @project.repository.nil? | ||
|
r8528 | @repository.project = @project | ||
end | ||||
def create | ||||
|
r9433 | attrs = pickup_extra_info | ||
|
r9693 | @repository = Repository.factory(params[:repository_scm]) | ||
@repository.safe_attributes = params[:repository] | ||||
|
r9433 | if attrs[:attrs_extra].keys.any? | ||
@repository.merge_extra_info(attrs[:attrs_extra]) | ||||
end | ||||
|
r8528 | @repository.project = @project | ||
if request.post? && @repository.save | ||||
redirect_to settings_project_path(@project, :tab => 'repositories') | ||||
else | ||||
render :action => 'new' | ||||
|
r714 | end | ||
|
r8528 | end | ||
def edit | ||||
end | ||||
def update | ||||
|
r9433 | attrs = pickup_extra_info | ||
|
r9693 | @repository.safe_attributes = attrs[:attrs] | ||
|
r9433 | if attrs[:attrs_extra].keys.any? | ||
@repository.merge_extra_info(attrs[:attrs_extra]) | ||||
end | ||||
|
r8528 | @repository.project = @project | ||
if request.put? && @repository.save | ||||
redirect_to settings_project_path(@project, :tab => 'repositories') | ||||
else | ||||
render :action => 'edit' | ||||
|
r3566 | end | ||
|
r714 | end | ||
|
r5497 | |||
|
r9433 | def pickup_extra_info | ||
p = {} | ||||
p_extra = {} | ||||
params[:repository].each do |k, v| | ||||
if k =~ /^extra_/ | ||||
p_extra[k] = v | ||||
else | ||||
p[k] = v | ||||
end | ||||
end | ||||
{:attrs => p, :attrs_extra => p_extra} | ||||
end | ||||
private :pickup_extra_info | ||||
|
r2004 | def committers | ||
@committers = @repository.committers | ||||
@users = @project.users | ||||
additional_user_ids = @committers.collect(&:last).collect(&:to_i) - @users.collect(&:id) | ||||
@users += User.find_all_by_id(additional_user_ids) unless additional_user_ids.empty? | ||||
@users.compact! | ||||
@users.sort! | ||||
|
r2135 | if request.post? && params[:committers].is_a?(Hash) | ||
# Build a hash with repository usernames as keys and corresponding user ids as values | ||||
@repository.committer_ids = params[:committers].values.inject({}) {|h, c| h[c.first] = c.last; h} | ||||
|
r2004 | flash[:notice] = l(:notice_successful_update) | ||
|
r8528 | redirect_to settings_project_path(@project, :tab => 'repositories') | ||
|
r2004 | end | ||
end | ||||
|
r4697 | |||
|
r714 | def destroy | ||
|
r8528 | @repository.destroy if request.delete? | ||
redirect_to settings_project_path(@project, :tab => 'repositories') | ||||
|
r714 | end | ||
|
r4697 | |||
def show | ||||
|
r2735 | @repository.fetch_changesets if Setting.autofetch_changesets? && @path.empty? | ||
|
r556 | @entries = @repository.entries(@path, @rev) | ||
|
r5019 | @changeset = @repository.find_changeset_by_name(@rev) | ||
|
r849 | if request.xhr? | ||
@entries ? render(:partial => 'dir_list_content') : render(:nothing => true) | ||||
else | ||||
|
r3071 | (show_error_not_found; return) unless @entries | ||
|
r2744 | @changesets = @repository.latest_changesets(@path, @rev) | ||
|
r1613 | @properties = @repository.properties(@path, @rev) | ||
|
r8530 | @repositories = @project.repositories | ||
|
r2735 | render :action => 'show' | ||
|
r849 | end | ||
|
r556 | end | ||
|
r2735 | |||
alias_method :browse, :show | ||||
|
r4697 | |||
|
r556 | def changes | ||
|
r1539 | @entry = @repository.entry(@path, @rev) | ||
|
r3071 | (show_error_not_found; return) unless @entry | ||
|
r2735 | @changesets = @repository.latest_changesets(@path, @rev, Setting.repository_log_display_limit.to_i) | ||
|
r1613 | @properties = @repository.properties(@path, @rev) | ||
|
r4613 | @changeset = @repository.find_changeset_by_name(@rev) | ||
|
r103 | end | ||
|
r4697 | |||
|
r103 | def revisions | ||
|
r556 | @changeset_count = @repository.changesets.count | ||
@changeset_pages = Paginator.new self, @changeset_count, | ||||
|
r4697 | per_page_option, | ||
params['page'] | ||||
|
r10687 | @changesets = @repository.changesets. | ||
limit(@changeset_pages.items_per_page). | ||||
offset(@changeset_pages.current.offset). | ||||
includes(:user, :repository, :parents). | ||||
all | ||||
|
r556 | |||
|
r663 | respond_to do |format| | ||
format.html { render :layout => false if request.xhr? } | ||||
format.atom { render_feed(@changesets, :title => "#{@project.name}: #{l(:label_revision_plural)}") } | ||||
end | ||||
|
r103 | end | ||
|
r4697 | |||
|
r9442 | def raw | ||
entry_and_raw(true) | ||||
end | ||||
|
r103 | def entry | ||
|
r9442 | entry_and_raw(false) | ||
end | ||||
def entry_and_raw(is_raw) | ||||
|
r1539 | @entry = @repository.entry(@path, @rev) | ||
|
r3071 | (show_error_not_found; return) unless @entry | ||
|
r1350 | # If the entry is a dir, show the browser | ||
|
r3071 | (show; return) if @entry.is_dir? | ||
|
r1539 | @content = @repository.cat(@path, @rev) | ||
|
r3071 | (show_error_not_found; return) unless @content | ||
|
r9442 | if is_raw || | ||
|
r5084 | (@content.size && @content.size > Setting.file_max_size_displayed.to_i.kilobyte) || | ||
! is_entry_text_data?(@content, @path) | ||||
|
r2442 | # Force the download | ||
|
r5090 | send_opt = { :filename => filename_for_content_disposition(@path.split('/').last) } | ||
send_type = Redmine::MimeType.of(@path) | ||||
send_opt[:type] = send_type.to_s if send_type | ||||
|
r9593 | send_opt[:disposition] = (Redmine::MimeType.is_type?('image', @path) && !is_raw ? 'inline' : 'attachment') | ||
|
r5090 | send_data @content, send_opt | ||
|
r980 | else | ||
# Prevent empty lines when displaying a file with Windows style eol | ||||
|
r5084 | # TODO: UTF-16 | ||
# Is this needs? AttachmentsController reads file simply. | ||||
|
r980 | @content.gsub!("\r\n", "\n") | ||
|
r4613 | @changeset = @repository.find_changeset_by_name(@rev) | ||
|
r5084 | end | ||
end | ||||
|
r9442 | private :entry_and_raw | ||
|
r5084 | |||
def is_entry_text_data?(ent, path) | ||||
# UTF-16 contains "\x00". | ||||
|
r5754 | # It is very strict that file contains less than 30% of ascii symbols | ||
|
r5084 | # in non Western Europe. | ||
return true if Redmine::MimeType.is_type?('text', path) | ||||
# Ruby 1.8.6 has a bug of integer divisions. | ||||
# http://apidock.com/ruby/v1_8_6_287/String/is_binary_data%3F | ||||
return false if ent.is_binary_data? | ||||
true | ||||
|
r103 | end | ||
|
r5084 | private :is_entry_text_data? | ||
|
r4613 | |||
|
r934 | def annotate | ||
|
r2165 | @entry = @repository.entry(@path, @rev) | ||
|
r3071 | (show_error_not_found; return) unless @entry | ||
|
r4697 | |||
|
r934 | @annotate = @repository.scm.annotate(@path, @rev) | ||
|
r7608 | if @annotate.nil? || @annotate.empty? | ||
(render_error l(:error_scm_annotate); return) | ||||
end | ||||
ann_buf_size = 0 | ||||
@annotate.lines.each do |buf| | ||||
ann_buf_size += buf.size | ||||
end | ||||
if ann_buf_size > Setting.file_max_size_displayed.to_i.kilobyte | ||||
(render_error l(:error_scm_annotate_big_text_file); return) | ||||
end | ||||
|
r4613 | @changeset = @repository.find_changeset_by_name(@rev) | ||
|
r934 | end | ||
|
r4613 | |||
|
r103 | def revision | ||
|
r925 | respond_to do |format| | ||
format.html | ||||
format.js {render :layout => false} | ||||
end | ||||
|
r8657 | end | ||
# Adds a related issue to a changeset | ||||
# POST /projects/:project_id/repository/(:repository_id/)revisions/:rev/issues | ||||
def add_related_issue | ||||
@issue = @changeset.find_referenced_issue_by_id(params[:issue_id]) | ||||
if @issue && (!@issue.visible? || @changeset.issues.include?(@issue)) | ||||
@issue = nil | ||||
end | ||||
if @issue | ||||
@changeset.issues << @issue | ||||
end | ||||
end | ||||
# Removes a related issue from a changeset | ||||
# DELETE /projects/:project_id/repository/(:repository_id/)revisions/:rev/issues/:issue_id | ||||
def remove_related_issue | ||||
@issue = Issue.visible.find_by_id(params[:issue_id]) | ||||
|
r10521 | if @issue | ||
|
r8657 | @changeset.issues.delete(@issue) | ||
end | ||||
|
r103 | end | ||
|
r4697 | |||
|
r103 | def diff | ||
|
r1500 | if params[:format] == 'diff' | ||
|
r1499 | @diff = @repository.diff(@path, @rev, @rev_to) | ||
|
r3071 | (show_error_not_found; return) unless @diff | ||
|
r1500 | filename = "changeset_r#{@rev}" | ||
filename << "_r#{@rev_to}" if @rev_to | ||||
send_data @diff.join, :filename => "#{filename}.diff", | ||||
:type => 'text/x-patch', | ||||
:disposition => 'attachment' | ||||
else | ||||
@diff_type = params[:type] || User.current.pref[:diff_type] || 'inline' | ||||
@diff_type = 'inline' unless %w(inline sbs).include?(@diff_type) | ||||
|
r5754 | |||
|
r1500 | # Save diff type as user preference | ||
if User.current.logged? && @diff_type != User.current.pref[:diff_type] | ||||
User.current.pref[:diff_type] = @diff_type | ||||
User.current.preference.save | ||||
end | ||||
|
r6771 | @cache_key = "repositories/diff/#{@repository.id}/" + | ||
|
r5953 | Digest::MD5.hexdigest("#{@path}-#{@rev}-#{@rev_to}-#{@diff_type}-#{current_language}") | ||
|
r1500 | unless read_fragment(@cache_key) | ||
@diff = @repository.diff(@path, @rev, @rev_to) | ||||
show_error_not_found unless @diff | ||||
end | ||||
|
r4493 | |||
@changeset = @repository.find_changeset_by_name(@rev) | ||||
@changeset_to = @rev_to ? @repository.find_changeset_by_name(@rev_to) : nil | ||||
|
r4578 | @diff_format_revisions = @repository.diff_format_revisions(@changeset, @changeset_to) | ||
|
r496 | end | ||
|
r103 | end | ||
|
r4578 | |||
|
r4697 | def stats | ||
|
r377 | end | ||
|
r4697 | |||
|
r377 | def graph | ||
|
r4697 | data = nil | ||
|
r377 | case params[:graph] | ||
when "commits_per_month" | ||||
data = graph_commits_per_month(@repository) | ||||
when "commits_per_author" | ||||
data = graph_commits_per_author(@repository) | ||||
end | ||||
if data | ||||
headers["Content-Type"] = "image/svg+xml" | ||||
send_data(data, :type => "image/svg+xml", :disposition => "inline") | ||||
else | ||||
render_404 | ||||
end | ||||
end | ||||
|
r5497 | |||
|
r4428 | private | ||
|
r8528 | def find_repository | ||
@repository = Repository.find(params[:id]) | ||||
@project = @repository.project | ||||
rescue ActiveRecord::RecordNotFound | ||||
render_404 | ||||
end | ||||
|
r4435 | REV_PARAM_RE = %r{\A[a-f0-9]*\Z}i | ||
|
r4428 | |||
|
r8528 | def find_project_repository | ||
|
r714 | @project = Project.find(params[:id]) | ||
|
r8530 | if params[:repository_id].present? | ||
@repository = @project.repositories.find_by_identifier_param(params[:repository_id]) | ||||
else | ||||
@repository = @project.repository | ||||
end | ||||
|
r3071 | (render_404; return false) unless @repository | ||
|
r9346 | @path = params[:path].is_a?(Array) ? params[:path].join('/') : params[:path].to_s | ||
|
r7502 | @rev = params[:rev].blank? ? @repository.default_branch : params[:rev].to_s.strip | ||
|
r1309 | @rev_to = params[:rev_to] | ||
|
r5497 | |||
|
r4740 | unless @rev.to_s.match(REV_PARAM_RE) && @rev_to.to_s.match(REV_PARAM_RE) | ||
|
r4428 | if @repository.branches.blank? | ||
raise InvalidRevisionParam | ||||
end | ||||
end | ||||
|
r130 | rescue ActiveRecord::RecordNotFound | ||
render_404 | ||||
|
r1309 | rescue InvalidRevisionParam | ||
show_error_not_found | ||||
|
r103 | end | ||
|
r8657 | def find_changeset | ||
if @rev.present? | ||||
@changeset = @repository.find_changeset_by_name(@rev) | ||||
end | ||||
show_error_not_found unless @changeset | ||||
end | ||||
|
r1080 | def show_error_not_found | ||
|
r4590 | render_error :message => l(:error_scm_not_found), :status => 404 | ||
|
r1080 | end | ||
|
r5497 | |||
|
r1541 | # Handler for Redmine::Scm::Adapters::CommandFailed exception | ||
def show_error_command_failed(exception) | ||||
render_error l(:error_scm_command_failed, exception.message) | ||||
|
r103 | end | ||
|
r5497 | |||
|
r377 | def graph_commits_per_month(repository) | ||
@date_to = Date.today | ||||
|
r639 | @date_from = @date_to << 11 | ||
@date_from = Date.civil(@date_from.year, @date_from.month, 1) | ||||
|
r9346 | commits_by_day = Changeset.count( | ||
|
r5497 | :all, :group => :commit_date, | ||
|
r9346 | :conditions => ["repository_id = ? AND commit_date BETWEEN ? AND ?", repository.id, @date_from, @date_to]) | ||
|
r377 | commits_by_month = [0] * 12 | ||
|
r9427 | commits_by_day.each {|c| commits_by_month[(@date_to.month - c.first.to_date.month) % 12] += c.last } | ||
|
r377 | |||
|
r9346 | changes_by_day = Change.count( | ||
:all, :group => :commit_date, :include => :changeset, | ||||
:conditions => ["#{Changeset.table_name}.repository_id = ? AND #{Changeset.table_name}.commit_date BETWEEN ? AND ?", repository.id, @date_from, @date_to]) | ||||
|
r377 | changes_by_month = [0] * 12 | ||
|
r9427 | changes_by_day.each {|c| changes_by_month[(@date_to.month - c.first.to_date.month) % 12] += c.last } | ||
|
r5497 | |||
|
r377 | fields = [] | ||
|
r2430 | 12.times {|m| fields << month_name(((Date.today.month - 1 - m) % 12) + 1)} | ||
|
r5497 | |||
|
r377 | graph = SVG::Graph::Bar.new( | ||
:height => 300, | ||||
|
r1587 | :width => 800, | ||
|
r377 | :fields => fields.reverse, | ||
:stack => :side, | ||||
:scale_integers => true, | ||||
:step_x_labels => 2, | ||||
:show_data_values => false, | ||||
:graph_title => l(:label_commits_per_month), | ||||
:show_graph_title => true | ||||
) | ||||
|
r5497 | |||
|
r377 | graph.add_data( | ||
:data => commits_by_month[0..11].reverse, | ||||
:title => l(:label_revision_plural) | ||||
) | ||||
graph.add_data( | ||||
:data => changes_by_month[0..11].reverse, | ||||
:title => l(:label_change_plural) | ||||
) | ||||
|
r5754 | |||
|
r377 | graph.burn | ||
end | ||||
def graph_commits_per_author(repository) | ||||
|
r9346 | commits_by_author = Changeset.count(:all, :group => :committer, :conditions => ["repository_id = ?", repository.id]) | ||
|
r2773 | commits_by_author.to_a.sort! {|x, y| x.last <=> y.last} | ||
|
r382 | |||
|
r9346 | changes_by_author = Change.count(:all, :group => :committer, :include => :changeset, :conditions => ["#{Changeset.table_name}.repository_id = ?", repository.id]) | ||
|
r382 | h = changes_by_author.inject({}) {|o, i| o[i.first] = i.last; o} | ||
|
r5754 | |||
|
r377 | fields = commits_by_author.collect {|r| r.first} | ||
|
r382 | commits_data = commits_by_author.collect {|r| r.last} | ||
changes_data = commits_by_author.collect {|r| h[r.first] || 0} | ||||
|
r5754 | |||
|
r377 | fields = fields + [""]*(10 - fields.length) if fields.length<10 | ||
|
r382 | commits_data = commits_data + [0]*(10 - commits_data.length) if commits_data.length<10 | ||
changes_data = changes_data + [0]*(10 - changes_data.length) if changes_data.length<10 | ||||
|
r5754 | |||
|
r1342 | # Remove email adress in usernames | ||
fields = fields.collect {|c| c.gsub(%r{<.+@.+>}, '') } | ||||
|
r5754 | |||
|
r377 | graph = SVG::Graph::BarHorizontal.new( | ||
|
r1587 | :height => 400, | ||
:width => 800, | ||||
|
r377 | :fields => fields, | ||
:stack => :side, | ||||
:scale_integers => true, | ||||
:show_data_values => false, | ||||
:rotate_y_labels => false, | ||||
:graph_title => l(:label_commits_per_author), | ||||
:show_graph_title => true | ||||
) | ||||
graph.add_data( | ||||
|
r382 | :data => commits_data, | ||
|
r377 | :title => l(:label_revision_plural) | ||
) | ||||
|
r382 | graph.add_data( | ||
:data => changes_data, | ||||
:title => l(:label_change_plural) | ||||
) | ||||
|
r377 | graph.burn | ||
end | ||||
|
r103 | end | ||