2006-12-24 16:38:45 +03:00
|
|
|
# redMine - project management software
|
2007-03-25 16:12:15 +04:00
|
|
|
# Copyright (C) 2006-2007 Jean-Philippe Lang
|
2006-12-24 16:38:45 +03:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
2007-03-25 21:11:46 +04:00
|
|
|
require 'SVG/Graph/Bar'
|
|
|
|
require 'SVG/Graph/BarHorizontal'
|
2007-05-02 00:56:19 +04:00
|
|
|
require 'digest/sha1'
|
2007-03-25 21:11:46 +04:00
|
|
|
|
2006-12-24 16:38:45 +03:00
|
|
|
class RepositoriesController < ApplicationController
|
|
|
|
layout 'base'
|
2007-09-14 15:34:08 +04:00
|
|
|
before_filter :find_repository, :except => :edit
|
|
|
|
before_filter :find_project, :only => :edit
|
|
|
|
before_filter :authorize
|
2007-08-29 20:52:35 +04:00
|
|
|
accept_key_auth :revisions
|
2007-03-25 21:11:46 +04:00
|
|
|
|
2007-09-14 15:34:08 +04:00
|
|
|
def edit
|
|
|
|
@repository = @project.repository
|
|
|
|
if !@repository
|
|
|
|
@repository = Repository.factory(params[:repository_scm])
|
|
|
|
@repository.project = @project
|
|
|
|
end
|
|
|
|
if request.post?
|
|
|
|
@repository.attributes = params[:repository]
|
|
|
|
@repository.save
|
|
|
|
end
|
|
|
|
render(:update) {|page| page.replace_html "tab-content-repository", :partial => 'projects/settings/repository'}
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
@repository.destroy
|
|
|
|
redirect_to :controller => 'projects', :action => 'settings', :id => @project, :tab => 'repository'
|
|
|
|
end
|
|
|
|
|
2006-12-24 16:38:45 +03:00
|
|
|
def show
|
2007-06-13 00:12:05 +04:00
|
|
|
# check if new revisions have been committed in the repository
|
|
|
|
@repository.fetch_changesets if Setting.autofetch_changesets?
|
2007-03-25 16:12:15 +04:00
|
|
|
# get entries for the browse frame
|
2007-06-29 22:50:48 +04:00
|
|
|
@entries = @repository.entries('')
|
2007-06-13 00:12:05 +04:00
|
|
|
# latest changesets
|
|
|
|
@changesets = @repository.changesets.find(:all, :limit => 10, :order => "committed_on DESC")
|
2007-06-29 22:50:48 +04:00
|
|
|
show_error and return unless @entries || @changesets.any?
|
2006-12-24 16:38:45 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def browse
|
2007-06-13 00:12:05 +04:00
|
|
|
@entries = @repository.entries(@path, @rev)
|
|
|
|
show_error and return unless @entries
|
|
|
|
end
|
|
|
|
|
|
|
|
def changes
|
|
|
|
@entry = @repository.scm.entry(@path, @rev)
|
|
|
|
show_error and return unless @entry
|
2007-10-15 22:41:27 +04:00
|
|
|
@changesets = @repository.changesets_for_path(@path)
|
2006-12-24 16:38:45 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def revisions
|
2007-06-13 00:12:05 +04:00
|
|
|
@changeset_count = @repository.changesets.count
|
|
|
|
@changeset_pages = Paginator.new self, @changeset_count,
|
|
|
|
25,
|
|
|
|
params['page']
|
|
|
|
@changesets = @repository.changesets.find(:all,
|
|
|
|
:limit => @changeset_pages.items_per_page,
|
|
|
|
:offset => @changeset_pages.current.offset)
|
|
|
|
|
2007-08-29 20:52:35 +04:00
|
|
|
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
|
2006-12-24 16:38:45 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def entry
|
2007-05-08 19:49:20 +04:00
|
|
|
@content = @repository.scm.cat(@path, @rev)
|
|
|
|
show_error and return unless @content
|
|
|
|
if 'raw' == params[:format]
|
|
|
|
send_data @content, :filename => @path.split('/').last
|
2006-12-24 16:38:45 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def revision
|
2007-03-25 16:12:15 +04:00
|
|
|
@changeset = @repository.changesets.find_by_revision(@rev)
|
|
|
|
show_error and return unless @changeset
|
2007-05-16 01:32:36 +04:00
|
|
|
@changes_count = @changeset.changes.size
|
|
|
|
@changes_pages = Paginator.new self, @changes_count, 150, params['page']
|
|
|
|
@changes = @changeset.changes.find(:all,
|
|
|
|
:limit => @changes_pages.items_per_page,
|
|
|
|
:offset => @changes_pages.current.offset)
|
|
|
|
|
|
|
|
render :action => "revision", :layout => false if request.xhr?
|
2006-12-24 16:38:45 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def diff
|
2007-06-13 00:12:05 +04:00
|
|
|
@rev_to = params[:rev_to] ? params[:rev_to].to_i : (@rev - 1)
|
2007-05-02 00:56:19 +04:00
|
|
|
@diff_type = ('sbs' == params[:type]) ? 'sbs' : 'inline'
|
|
|
|
|
|
|
|
@cache_key = "repositories/diff/#{@repository.id}/" + Digest::MD5.hexdigest("#{@path}-#{@rev}-#{@rev_to}-#{@diff_type}")
|
|
|
|
unless read_fragment(@cache_key)
|
2007-06-13 00:12:05 +04:00
|
|
|
@diff = @repository.diff(@path, @rev, @rev_to, type)
|
2007-05-02 00:56:19 +04:00
|
|
|
show_error and return unless @diff
|
|
|
|
end
|
2006-12-24 16:38:45 +03:00
|
|
|
end
|
|
|
|
|
2007-03-25 21:11:46 +04:00
|
|
|
def stats
|
|
|
|
end
|
|
|
|
|
|
|
|
def graph
|
|
|
|
data = nil
|
|
|
|
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
|
|
|
|
|
2006-12-24 16:38:45 +03:00
|
|
|
private
|
|
|
|
def find_project
|
|
|
|
@project = Project.find(params[:id])
|
2007-09-14 15:34:08 +04:00
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
render_404
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_repository
|
|
|
|
@project = Project.find(params[:id])
|
2006-12-24 16:38:45 +03:00
|
|
|
@repository = @project.repository
|
2007-03-11 17:06:05 +03:00
|
|
|
render_404 and return false unless @repository
|
2007-05-10 22:39:49 +04:00
|
|
|
@path = params[:path].squeeze('/') if params[:path]
|
2006-12-24 16:38:45 +03:00
|
|
|
@path ||= ''
|
2007-06-13 00:12:05 +04:00
|
|
|
@rev = params[:rev].to_i if params[:rev]
|
2007-01-02 11:47:07 +03:00
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
render_404
|
2006-12-24 16:38:45 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def show_error
|
2007-08-02 21:42:20 +04:00
|
|
|
flash.now[:error] = l(:notice_scm_error)
|
2006-12-24 16:38:45 +03:00
|
|
|
render :nothing => true, :layout => true
|
|
|
|
end
|
2007-03-25 21:11:46 +04:00
|
|
|
|
|
|
|
def graph_commits_per_month(repository)
|
|
|
|
@date_to = Date.today
|
2007-08-16 03:04:14 +04:00
|
|
|
@date_from = @date_to << 11
|
|
|
|
@date_from = Date.civil(@date_from.year, @date_from.month, 1)
|
2007-03-25 21:11:46 +04:00
|
|
|
commits_by_day = repository.changesets.count(:all, :group => :commit_date, :conditions => ["commit_date BETWEEN ? AND ?", @date_from, @date_to])
|
|
|
|
commits_by_month = [0] * 12
|
|
|
|
commits_by_day.each {|c| commits_by_month[c.first.to_date.months_ago] += c.last }
|
|
|
|
|
2007-08-16 03:04:14 +04:00
|
|
|
changes_by_day = repository.changes.count(:all, :group => :commit_date, :conditions => ["commit_date BETWEEN ? AND ?", @date_from, @date_to])
|
2007-03-25 21:11:46 +04:00
|
|
|
changes_by_month = [0] * 12
|
|
|
|
changes_by_day.each {|c| changes_by_month[c.first.to_date.months_ago] += c.last }
|
|
|
|
|
|
|
|
fields = []
|
|
|
|
month_names = l(:actionview_datehelper_select_month_names_abbr).split(',')
|
|
|
|
12.times {|m| fields << month_names[((Date.today.month - 1 - m) % 12)]}
|
|
|
|
|
|
|
|
graph = SVG::Graph::Bar.new(
|
|
|
|
:height => 300,
|
|
|
|
:width => 500,
|
|
|
|
: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
|
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
)
|
|
|
|
|
|
|
|
graph.burn
|
|
|
|
end
|
|
|
|
|
|
|
|
def graph_commits_per_author(repository)
|
|
|
|
commits_by_author = repository.changesets.count(:all, :group => :committer)
|
|
|
|
commits_by_author.sort! {|x, y| x.last <=> y.last}
|
2007-03-26 21:14:06 +04:00
|
|
|
|
|
|
|
changes_by_author = repository.changes.count(:all, :group => :committer)
|
|
|
|
h = changes_by_author.inject({}) {|o, i| o[i.first] = i.last; o}
|
2007-03-25 21:11:46 +04:00
|
|
|
|
|
|
|
fields = commits_by_author.collect {|r| r.first}
|
2007-03-26 21:14:06 +04:00
|
|
|
commits_data = commits_by_author.collect {|r| r.last}
|
|
|
|
changes_data = commits_by_author.collect {|r| h[r.first] || 0}
|
2007-03-25 21:11:46 +04:00
|
|
|
|
|
|
|
fields = fields + [""]*(10 - fields.length) if fields.length<10
|
2007-03-26 21:14:06 +04:00
|
|
|
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
|
2007-03-25 21:11:46 +04:00
|
|
|
|
|
|
|
graph = SVG::Graph::BarHorizontal.new(
|
|
|
|
:height => 300,
|
|
|
|
:width => 500,
|
|
|
|
: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(
|
2007-03-26 21:14:06 +04:00
|
|
|
:data => commits_data,
|
2007-03-25 21:11:46 +04:00
|
|
|
:title => l(:label_revision_plural)
|
|
|
|
)
|
2007-03-26 21:14:06 +04:00
|
|
|
|
|
|
|
graph.add_data(
|
|
|
|
:data => changes_data,
|
|
|
|
:title => l(:label_change_plural)
|
|
|
|
)
|
|
|
|
|
2007-03-25 21:11:46 +04:00
|
|
|
graph.burn
|
|
|
|
end
|
|
|
|
|
2006-12-24 16:38:45 +03:00
|
|
|
end
|
2007-03-25 21:11:46 +04:00
|
|
|
|
|
|
|
class Date
|
|
|
|
def months_ago(date = Date.today)
|
|
|
|
(date.year - self.year)*12 + (date.month - self.month)
|
|
|
|
end
|
|
|
|
|
|
|
|
def weeks_ago(date = Date.today)
|
|
|
|
(date.year - self.year)*52 + (date.cweek - self.cweek)
|
|
|
|
end
|
2007-03-29 22:14:59 +04:00
|
|
|
end
|
2007-06-13 00:12:05 +04:00
|
|
|
|
|
|
|
class String
|
|
|
|
def with_leading_slash
|
|
|
|
starts_with?('/') ? self : "/#{self}"
|
|
|
|
end
|
|
|
|
end
|