obsolete.ChilliProject/app/models/repository.rb

296 lines
8.3 KiB
Ruby
Raw Normal View History

2011-10-29 16:19:11 +04:00
#-- encoding: UTF-8
#-- copyright
# ChiliProject is a project management system.
2011-05-30 22:52:25 +04:00
#
# Copyright (C) 2010-2013 the ChiliProject Team
2011-05-30 22:52:25 +04: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.
2011-05-30 22:52:25 +04:00
#
# See doc/COPYRIGHT.rdoc for more details.
#++
class Repository < ActiveRecord::Base
include Redmine::Ciphering
2011-05-30 22:52:25 +04:00
belongs_to :project
has_many :changesets, :order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC"
has_many :changes, :through => :changesets
2011-05-30 22:52:25 +04:00
# Raw SQL to delete changesets and changes in the database
# has_many :changesets, :dependent => :destroy is too slow for big repositories
before_destroy :clear_changesets
2011-05-30 22:52:25 +04:00
validates_length_of :password, :maximum => 255, :allow_nil => true
# Checks if the SCM is enabled when creating a repository
validate_on_create { |r| r.errors.add(:type, :invalid) unless Setting.enabled_scm.include?(r.class.name.demodulize) }
# Removes leading and trailing whitespace
def url=(arg)
write_attribute(:url, arg ? arg.to_s.strip : nil)
end
# Removes leading and trailing whitespace
def root_url=(arg)
write_attribute(:root_url, arg ? arg.to_s.strip : nil)
end
2011-05-30 22:52:25 +04:00
def password
read_ciphered_attribute(:password)
end
2011-05-30 22:52:25 +04:00
def password=(arg)
write_ciphered_attribute(:password, arg)
end
def scm_adapter
self.class.scm_adapter_class
end
def scm
@scm ||= self.scm_adapter.new(url, root_url,
login, password, path_encoding)
update_attribute(:root_url, @scm.root_url) if root_url.blank?
@scm
end
def scm_name
self.class.scm_name
end
def supports_cat?
scm.supports_cat?
end
def supports_annotate?
scm.supports_annotate?
end
def supports_all_revisions?
true
end
2011-05-30 22:52:25 +04:00
def supports_directory_revisions?
false
end
2011-05-30 22:52:25 +04:00
def entry(path=nil, identifier=nil)
scm.entry(path, identifier)
end
2011-05-30 22:52:25 +04:00
def entries(path=nil, identifier=nil)
scm.entries(path, identifier)
end
Added branch and tag support to the git repository viewer. (#1406) Many thanks to Adam Soltys and everyone else who tested this patch. * Updated git test repository so it has a branch with some differences from the master branch * Moved redmine diff class into a module so as not to clash with diff-lcs gem which is required by grit * Find changesets from all branches, not just master * Got revision browsing working * Got file actions working properly * Allow browsing by short form of commit identifier * Added a method to retrieve repository branches * Allow browsing by branch names as well as commit numbers * Handle the case where a git repository has no master branch * Expand revision box and handle finding revisions by first 8 characters * Added branches dropdown to repository show page * Combined repository browse and show into a single action. Moved branch/revision navigation into a partial. * Renamed partial navigation -> breadcrumbs * Made it so latest revisions list uses branch and path context * Preserve current path when changing branch or revision * Perform slightly more graceful error handling in the case of invalid repository URLs * Allow branch names to contain periods * Allow dashes in branch names * Sort branches by name * Adding tags dropdown * Need to disable both branches and tags dropdowns before submitting revision form * Support underscores in revision (branch/tag) names * Making file history sensitive to current branch/tag/revision, adding common navigation to changes page * Updated translation files to include labels for 'branch', 'tag', and 'view all revisions' * Reenable fields after submit so they don't look disabled and don't stay disabled on browser back button * Instead of dashes just use empty string for default dropdown value * Individual entry views now sport the upgraded revision navigation * Don't display dropdowns with no entries * Consider all revisions when doing initial load * Fixed bug grabbing changesets. Thanks to Bernhard Furtmueller for catching. * Always check the entire log to find new revisions, rather than trying to go forward from the latest known one * Added some cleverness to avoid selecting the whole changesets table any time someone views the repository root * File copies and renames being detected properly * Return gracefully if no revisions are found in the git log * Applied patch from Babar Le Lapin to improve Windows compatibility git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@2840 e93f8b46-1217-0410-a6f0-8f06a7374b81
2009-08-16 02:41:40 +04:00
def branches
scm.branches
end
def tags
scm.tags
end
def default_branch
scm.default_branch
end
def properties(path, identifier=nil)
scm.properties(path, identifier)
end
def cat(path, identifier=nil)
scm.cat(path, identifier)
end
def diff(path, rev, rev_to)
scm.diff(path, rev, rev_to)
end
def diff_format_revisions(cs, cs_to, sep=':')
text = ""
text << cs_to.format_identifier + sep if cs_to
text << cs.format_identifier if cs
text
end
# Returns a path relative to the url of the repository
def relative_path(path)
path
end
# Finds and returns a revision with a number or the beginning of a hash
def find_changeset_by_name(name)
return nil if name.blank?
changesets.find(:first, :conditions => (name.match(/^\d*$/) ? ["revision = ?", name.to_s] : ["revision LIKE ?", name + '%']))
end
def latest_changeset
@latest_changeset ||= changesets.find(:first)
end
Added branch and tag support to the git repository viewer. (#1406) Many thanks to Adam Soltys and everyone else who tested this patch. * Updated git test repository so it has a branch with some differences from the master branch * Moved redmine diff class into a module so as not to clash with diff-lcs gem which is required by grit * Find changesets from all branches, not just master * Got revision browsing working * Got file actions working properly * Allow browsing by short form of commit identifier * Added a method to retrieve repository branches * Allow browsing by branch names as well as commit numbers * Handle the case where a git repository has no master branch * Expand revision box and handle finding revisions by first 8 characters * Added branches dropdown to repository show page * Combined repository browse and show into a single action. Moved branch/revision navigation into a partial. * Renamed partial navigation -> breadcrumbs * Made it so latest revisions list uses branch and path context * Preserve current path when changing branch or revision * Perform slightly more graceful error handling in the case of invalid repository URLs * Allow branch names to contain periods * Allow dashes in branch names * Sort branches by name * Adding tags dropdown * Need to disable both branches and tags dropdowns before submitting revision form * Support underscores in revision (branch/tag) names * Making file history sensitive to current branch/tag/revision, adding common navigation to changes page * Updated translation files to include labels for 'branch', 'tag', and 'view all revisions' * Reenable fields after submit so they don't look disabled and don't stay disabled on browser back button * Instead of dashes just use empty string for default dropdown value * Individual entry views now sport the upgraded revision navigation * Don't display dropdowns with no entries * Consider all revisions when doing initial load * Fixed bug grabbing changesets. Thanks to Bernhard Furtmueller for catching. * Always check the entire log to find new revisions, rather than trying to go forward from the latest known one * Added some cleverness to avoid selecting the whole changesets table any time someone views the repository root * File copies and renames being detected properly * Return gracefully if no revisions are found in the git log * Applied patch from Babar Le Lapin to improve Windows compatibility git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@2840 e93f8b46-1217-0410-a6f0-8f06a7374b81
2009-08-16 02:41:40 +04:00
# Returns the latest changesets for +path+
# Default behaviour is to search in cached changesets
def latest_changesets(path, rev, limit=10)
if path.blank?
changesets.find(:all, :include => :user,
:order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC",
:limit => limit)
else
2011-05-30 22:52:25 +04:00
changes.find(:all, :include => {:changeset => :user},
:conditions => ["path = ?", path.with_leading_slash],
:order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC",
:limit => limit).collect(&:changeset)
end
Added branch and tag support to the git repository viewer. (#1406) Many thanks to Adam Soltys and everyone else who tested this patch. * Updated git test repository so it has a branch with some differences from the master branch * Moved redmine diff class into a module so as not to clash with diff-lcs gem which is required by grit * Find changesets from all branches, not just master * Got revision browsing working * Got file actions working properly * Allow browsing by short form of commit identifier * Added a method to retrieve repository branches * Allow browsing by branch names as well as commit numbers * Handle the case where a git repository has no master branch * Expand revision box and handle finding revisions by first 8 characters * Added branches dropdown to repository show page * Combined repository browse and show into a single action. Moved branch/revision navigation into a partial. * Renamed partial navigation -> breadcrumbs * Made it so latest revisions list uses branch and path context * Preserve current path when changing branch or revision * Perform slightly more graceful error handling in the case of invalid repository URLs * Allow branch names to contain periods * Allow dashes in branch names * Sort branches by name * Adding tags dropdown * Need to disable both branches and tags dropdowns before submitting revision form * Support underscores in revision (branch/tag) names * Making file history sensitive to current branch/tag/revision, adding common navigation to changes page * Updated translation files to include labels for 'branch', 'tag', and 'view all revisions' * Reenable fields after submit so they don't look disabled and don't stay disabled on browser back button * Instead of dashes just use empty string for default dropdown value * Individual entry views now sport the upgraded revision navigation * Don't display dropdowns with no entries * Consider all revisions when doing initial load * Fixed bug grabbing changesets. Thanks to Bernhard Furtmueller for catching. * Always check the entire log to find new revisions, rather than trying to go forward from the latest known one * Added some cleverness to avoid selecting the whole changesets table any time someone views the repository root * File copies and renames being detected properly * Return gracefully if no revisions are found in the git log * Applied patch from Babar Le Lapin to improve Windows compatibility git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@2840 e93f8b46-1217-0410-a6f0-8f06a7374b81
2009-08-16 02:41:40 +04:00
end
2011-05-30 22:52:25 +04:00
def scan_changesets_for_issue_ids
self.changesets.each(&:scan_comment_for_issue_ids)
end
Added branch and tag support to the git repository viewer. (#1406) Many thanks to Adam Soltys and everyone else who tested this patch. * Updated git test repository so it has a branch with some differences from the master branch * Moved redmine diff class into a module so as not to clash with diff-lcs gem which is required by grit * Find changesets from all branches, not just master * Got revision browsing working * Got file actions working properly * Allow browsing by short form of commit identifier * Added a method to retrieve repository branches * Allow browsing by branch names as well as commit numbers * Handle the case where a git repository has no master branch * Expand revision box and handle finding revisions by first 8 characters * Added branches dropdown to repository show page * Combined repository browse and show into a single action. Moved branch/revision navigation into a partial. * Renamed partial navigation -> breadcrumbs * Made it so latest revisions list uses branch and path context * Preserve current path when changing branch or revision * Perform slightly more graceful error handling in the case of invalid repository URLs * Allow branch names to contain periods * Allow dashes in branch names * Sort branches by name * Adding tags dropdown * Need to disable both branches and tags dropdowns before submitting revision form * Support underscores in revision (branch/tag) names * Making file history sensitive to current branch/tag/revision, adding common navigation to changes page * Updated translation files to include labels for 'branch', 'tag', and 'view all revisions' * Reenable fields after submit so they don't look disabled and don't stay disabled on browser back button * Instead of dashes just use empty string for default dropdown value * Individual entry views now sport the upgraded revision navigation * Don't display dropdowns with no entries * Consider all revisions when doing initial load * Fixed bug grabbing changesets. Thanks to Bernhard Furtmueller for catching. * Always check the entire log to find new revisions, rather than trying to go forward from the latest known one * Added some cleverness to avoid selecting the whole changesets table any time someone views the repository root * File copies and renames being detected properly * Return gracefully if no revisions are found in the git log * Applied patch from Babar Le Lapin to improve Windows compatibility git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@2840 e93f8b46-1217-0410-a6f0-8f06a7374b81
2009-08-16 02:41:40 +04:00
# Returns an array of committers usernames and associated user_id
def committers
@committers ||= Changeset.connection.select_rows("SELECT DISTINCT committer, user_id FROM #{Changeset.table_name} WHERE repository_id = #{id}")
end
2011-05-30 22:52:25 +04:00
# Maps committers username to a user ids
def committer_ids=(h)
if h.is_a?(Hash)
committers.each do |committer, user_id|
new_user_id = h[committer]
if new_user_id && (new_user_id.to_i != user_id.to_i)
new_user_id = (new_user_id.to_i > 0 ? new_user_id.to_i : nil)
Changeset.update_all("user_id = #{ new_user_id.nil? ? 'NULL' : new_user_id }", ["repository_id = ? AND committer = ?", id, committer])
end
end
@committers = nil
@found_committer_users = nil
true
else
false
end
end
2011-05-30 22:52:25 +04:00
# Returns the Redmine User corresponding to the given +committer+
# It will return nil if the committer is not yet mapped and if no User
# with the same username or email was found
def find_committer_user(committer)
unless committer.blank?
@found_committer_users ||= {}
return @found_committer_users[committer] if @found_committer_users.has_key?(committer)
2011-05-30 22:52:25 +04:00
user = nil
c = changesets.find(:first, :conditions => {:committer => committer}, :include => :user)
if c && c.user
user = c.user
elsif committer.strip =~ /^([^<]+)(<(.*)>)?$/
username, email = $1.strip, $3
u = User.find_by_login(username)
u ||= User.find_by_mail(email) unless email.blank?
user = u
end
@found_committer_users[committer] = user
user
end
end
def repo_log_encoding
encoding = log_encoding.to_s.strip
encoding.blank? ? 'UTF-8' : encoding
end
# Provide a log encoding even if the column was not created yet
# It's used by 20100714111653_build_initial_journals_for_acts_as_journalized
# for initial journal creaetion of the changesets while the colum is only
# created by 20110228000000_add_repositories_log_encoding
def log_encoding
read_attribute(:log_encoding)
end
# Fetches new changesets for all repositories of active projects
# Can be called periodically by an external script
# eg. ruby script/runner "Repository.fetch_changesets"
def self.fetch_changesets
Project.active.has_module(:repository).find(:all, :include => :repository).each do |project|
if project.repository
begin
project.repository.fetch_changesets
rescue Redmine::Scm::Adapters::CommandFailed => e
logger.error "scm: error during fetching changesets: #{e.message}"
end
end
end
end
# scan changeset comments to find related and fixed issues for all repositories
def self.scan_changesets_for_issue_ids
find(:all).each(&:scan_changesets_for_issue_ids)
end
def self.scm_name
'Abstract'
end
2011-05-30 22:52:25 +04:00
def self.available_scm
subclasses.collect {|klass| [klass.scm_name, klass.name]}
end
def self.factory(klass_name, *args)
klass = "Repository::#{klass_name}".constantize
klass.new(*args)
rescue
nil
end
def self.scm_adapter_class
nil
end
def self.scm_command
ret = ""
begin
ret = self.scm_adapter_class.client_command if self.scm_adapter_class
rescue Redmine::Scm::Adapters::CommandFailed => e
logger.error "scm: error during get command: #{e.message}"
end
ret
end
def self.scm_version_string
ret = ""
begin
ret = self.scm_adapter_class.client_version_string if self.scm_adapter_class
rescue Redmine::Scm::Adapters::CommandFailed => e
logger.error "scm: error during get version string: #{e.message}"
end
ret
end
def self.scm_available
ret = false
begin
2011-05-30 22:52:25 +04:00
ret = self.scm_adapter_class.client_available if self.scm_adapter_class
rescue Redmine::Scm::Adapters::CommandFailed => e
logger.error "scm: error during get scm available: #{e.message}"
end
ret
end
private
def before_save
# Strips url and root_url
url.strip!
root_url.strip!
true
end
2011-05-30 22:52:25 +04:00
def clear_changesets
cs, ch, ci = Changeset.table_name, Change.table_name, "#{table_name_prefix}changesets_issues#{table_name_suffix}"
connection.delete("DELETE FROM #{ch} WHERE #{ch}.changeset_id IN (SELECT #{cs}.id FROM #{cs} WHERE #{cs}.repository_id = #{id})")
connection.delete("DELETE FROM #{ci} WHERE #{ci}.changeset_id IN (SELECT #{cs}.id FROM #{cs} WHERE #{cs}.repository_id = #{id})")
connection.delete("DELETE FROM #{cs} WHERE #{cs}.repository_id = #{id}")
end
end