Chris@0: # redMine - project management software Chris@0: # Copyright (C) 2006-2007 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@0: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@0: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@0: require 'redmine/scm/adapters/mercurial_adapter' Chris@0: Chris@0: class Repository::Mercurial < Repository Chris@0: attr_protected :root_url luis@225: # validates_presence_of :url Chris@0: Chris@3: FETCH_AT_ONCE = 100 # number of changesets to fetch at once Chris@3: Chris@0: def scm_adapter Chris@0: Redmine::Scm::Adapters::MercurialAdapter Chris@0: end Chris@0: Chris@0: def self.scm_name Chris@0: 'Mercurial' Chris@0: end Chris@0: Chris@0: def entries(path=nil, identifier=nil) Chris@3: scm.entries(path, identifier) Chris@3: end Chris@3: Chris@3: def branches Chris@3: bras = scm.branches Chris@3: bras.sort unless bras == %w|default| Chris@3: end Chris@3: Chris@3: # Returns the latest changesets for +path+ Chris@3: def latest_changesets(path, rev, limit=10) Chris@3: changesets.find(:all, :include => :user, Chris@3: :conditions => latest_changesets_cond(path, rev, limit), Chris@3: :limit => limit) Chris@3: end Chris@3: Chris@3: def latest_changesets_cond(path, rev, limit) Chris@3: cond, args = [], [] Chris@3: Chris@3: if scm.branchmap.member? rev Chris@3: # dirty hack to filter by branch. branch name should be in database. Chris@3: cond << "#{Changeset.table_name}.scmid IN (?)" Chris@3: args << scm.nodes_in_branch(rev, path, rev, 0, :limit => limit) Chris@3: elsif last = rev ? find_changeset_by_name(scm.tagmap[rev] || rev) : nil Chris@3: cond << "#{Changeset.table_name}.id <= ?" Chris@3: args << last.id Chris@3: end Chris@3: Chris@3: unless path.blank? Chris@3: # TODO: there must be a better way to build sub-query Chris@3: cond << "EXISTS (SELECT * FROM #{Change.table_name} Chris@3: WHERE #{Change.table_name}.changeset_id = #{Changeset.table_name}.id Chris@3: AND (#{Change.table_name}.path = ? OR #{Change.table_name}.path LIKE ?))" Chris@3: args << path.with_leading_slash << "#{path.with_leading_slash}/%" Chris@3: end Chris@3: Chris@3: [cond.join(' AND '), *args] unless cond.empty? Chris@3: end Chris@3: private :latest_changesets_cond Chris@3: Chris@3: def fetch_changesets Chris@3: scm_rev = scm.info.lastrev.revision.to_i Chris@3: db_rev = latest_changeset ? latest_changeset.revision.to_i : -1 Chris@3: return unless db_rev < scm_rev # already up-to-date Chris@3: Chris@3: logger.debug "Fetching changesets for repository #{url}" if logger Chris@3: (db_rev + 1).step(scm_rev, FETCH_AT_ONCE) do |i| Chris@3: transaction do Chris@3: scm.each_revision('', i, [i + FETCH_AT_ONCE - 1, scm_rev].min) do |re| Chris@3: cs = Changeset.create(:repository => self, Chris@3: :revision => re.revision, Chris@3: :scmid => re.scmid, Chris@3: :committer => re.author, Chris@3: :committed_on => re.time, Chris@3: :comments => re.message) Chris@3: re.paths.each { |e| cs.create_change(e) } Chris@0: end Chris@0: end Chris@0: end Chris@3: self Chris@0: end Chris@0: end