annotate app/models/repository/mercurial.rb @ 119:8661b858af72

* Update to Redmine trunk rev 4705
author Chris Cannam
date Thu, 13 Jan 2011 14:12:06 +0000
parents 513646585e45
children cd2282d2aa55 051f544170fe
rev   line source
Chris@0 1 # redMine - project management software
Chris@0 2 # Copyright (C) 2006-2007 Jean-Philippe Lang
Chris@0 3 #
Chris@0 4 # This program is free software; you can redistribute it and/or
Chris@0 5 # modify it under the terms of the GNU General Public License
Chris@0 6 # as published by the Free Software Foundation; either version 2
Chris@0 7 # of the License, or (at your option) any later version.
Chris@0 8 #
Chris@0 9 # This program is distributed in the hope that it will be useful,
Chris@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 # GNU General Public License for more details.
Chris@0 13 #
Chris@0 14 # You should have received a copy of the GNU General Public License
Chris@0 15 # along with this program; if not, write to the Free Software
Chris@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 17
Chris@0 18 require 'redmine/scm/adapters/mercurial_adapter'
Chris@0 19
Chris@0 20 class Repository::Mercurial < Repository
Chris@119 21 # sort changesets by revision number
Chris@119 22 has_many :changesets, :order => "#{Changeset.table_name}.id DESC", :foreign_key => 'repository_id'
Chris@119 23
Chris@0 24 attr_protected :root_url
Chris@0 25 validates_presence_of :url
Chris@0 26
Chris@0 27 def scm_adapter
Chris@0 28 Redmine::Scm::Adapters::MercurialAdapter
Chris@0 29 end
Chris@119 30
Chris@0 31 def self.scm_name
Chris@0 32 'Mercurial'
Chris@0 33 end
Chris@119 34
Chris@119 35 # Returns the readable identifier for the given mercurial changeset
Chris@119 36 def self.format_changeset_identifier(changeset)
Chris@119 37 "#{changeset.revision}:#{changeset.scmid}"
Chris@119 38 end
Chris@119 39
Chris@119 40 # Returns the identifier for the given Mercurial changeset
Chris@119 41 def self.changeset_identifier(changeset)
Chris@119 42 changeset.scmid
Chris@119 43 end
Chris@119 44
Chris@119 45 def diff_format_revisions(cs, cs_to, sep=':')
Chris@119 46 super(cs, cs_to, ' ')
Chris@119 47 end
Chris@119 48
Chris@0 49 def entries(path=nil, identifier=nil)
Chris@0 50 entries=scm.entries(path, identifier)
Chris@0 51 if entries
Chris@0 52 entries.each do |entry|
Chris@0 53 next unless entry.is_file?
Chris@0 54 # Set the filesize unless browsing a specific revision
Chris@0 55 if identifier.nil?
Chris@0 56 full_path = File.join(root_url, entry.path)
Chris@0 57 entry.size = File.stat(full_path).size if File.file?(full_path)
Chris@0 58 end
Chris@0 59 # Search the DB for the entry's last change
Chris@0 60 change = changes.find(:first, :conditions => ["path = ?", scm.with_leading_slash(entry.path)], :order => "#{Changeset.table_name}.committed_on DESC")
Chris@0 61 if change
Chris@0 62 entry.lastrev.identifier = change.changeset.revision
Chris@0 63 entry.lastrev.name = change.changeset.revision
Chris@0 64 entry.lastrev.author = change.changeset.committer
Chris@0 65 entry.lastrev.revision = change.revision
Chris@0 66 end
Chris@0 67 end
Chris@0 68 end
Chris@0 69 entries
Chris@0 70 end
Chris@0 71
Chris@119 72 # Finds and returns a revision with a number or the beginning of a hash
Chris@119 73 def find_changeset_by_name(name)
Chris@119 74 return nil if name.nil? || name.empty?
Chris@119 75 if /[^\d]/ =~ name or name.to_s.size > 8
Chris@119 76 e = changesets.find(:first, :conditions => ['scmid = ?', name.to_s])
Chris@119 77 else
Chris@119 78 e = changesets.find(:first, :conditions => ['revision = ?', name.to_s])
Chris@119 79 end
Chris@119 80 return e if e
Chris@119 81 changesets.find(:first, :conditions => ['scmid LIKE ?', "#{name}%"]) # last ditch
Chris@119 82 end
Chris@119 83
Chris@119 84 # Returns the latest changesets for +path+; sorted by revision number
Chris@119 85 def latest_changesets(path, rev, limit=10)
Chris@119 86 if path.blank?
Chris@119 87 changesets.find(:all, :include => :user, :limit => limit)
Chris@119 88 else
Chris@119 89 changes.find(:all, :include => {:changeset => :user},
Chris@119 90 :conditions => ["path = ?", path.with_leading_slash],
Chris@119 91 :order => "#{Changeset.table_name}.id DESC",
Chris@119 92 :limit => limit).collect(&:changeset)
Chris@119 93 end
Chris@119 94 end
Chris@119 95
Chris@0 96 def fetch_changesets
Chris@0 97 scm_info = scm.info
Chris@0 98 if scm_info
Chris@0 99 # latest revision found in database
Chris@0 100 db_revision = latest_changeset ? latest_changeset.revision.to_i : -1
Chris@0 101 # latest revision in the repository
Chris@0 102 latest_revision = scm_info.lastrev
Chris@0 103 return if latest_revision.nil?
Chris@0 104 scm_revision = latest_revision.identifier.to_i
Chris@0 105 if db_revision < scm_revision
Chris@0 106 logger.debug "Fetching changesets for repository #{url}" if logger && logger.debug?
Chris@0 107 identifier_from = db_revision + 1
Chris@0 108 while (identifier_from <= scm_revision)
Chris@0 109 # loads changesets by batches of 100
Chris@0 110 identifier_to = [identifier_from + 99, scm_revision].min
Chris@0 111 revisions = scm.revisions('', identifier_from, identifier_to, :with_paths => true)
Chris@0 112 transaction do
Chris@0 113 revisions.each do |revision|
Chris@0 114 changeset = Changeset.create(:repository => self,
Chris@0 115 :revision => revision.identifier,
Chris@0 116 :scmid => revision.scmid,
Chris@0 117 :committer => revision.author,
Chris@0 118 :committed_on => revision.time,
Chris@0 119 :comments => revision.message)
Chris@0 120
Chris@0 121 revision.paths.each do |change|
Chris@0 122 changeset.create_change(change)
Chris@0 123 end
Chris@0 124 end
Chris@0 125 end unless revisions.nil?
Chris@0 126 identifier_from = identifier_to + 1
Chris@0 127 end
Chris@0 128 end
Chris@0 129 end
Chris@0 130 end
Chris@0 131 end