Chris@441: # Redmine - project management software Chris@1115: # Copyright (C) 2006-2012 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@441: # 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@441: # 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@1136: require_dependency 'redmine/scm/adapters/mercurial_adapter' Chris@0: Chris@0: class Repository::Mercurial < Repository Chris@119: # sort changesets by revision number Chris@909: has_many :changesets, Chris@909: :order => "#{Changeset.table_name}.id DESC", Chris@909: :foreign_key => 'repository_id' Chris@119: Chris@909: attr_protected :root_url luis@225: # validates_presence_of :url Chris@0: Chris@909: # number of changesets to fetch at once Chris@909: FETCH_AT_ONCE = 100 Chris@245: Chris@1115: def self.human_attribute_name(attribute_key_name, *args) Chris@1115: attr_name = attribute_key_name.to_s Chris@441: if attr_name == "url" Chris@441: attr_name = "path_to_repository" Chris@441: end Chris@1115: super(attr_name, *args) Chris@245: end Chris@245: Chris@245: def self.scm_adapter_class Chris@0: Redmine::Scm::Adapters::MercurialAdapter Chris@0: end Chris@119: Chris@0: def self.scm_name Chris@0: 'Mercurial' Chris@0: end Chris@119: Chris@441: def supports_directory_revisions? Chris@441: true Chris@441: end Chris@441: Chris@909: def supports_revision_graph? Chris@909: true Chris@909: end Chris@909: Chris@245: def repo_log_encoding Chris@245: 'UTF-8' Chris@245: end Chris@245: Chris@119: # Returns the readable identifier for the given mercurial changeset Chris@119: def self.format_changeset_identifier(changeset) Chris@119: "#{changeset.revision}:#{changeset.scmid}" Chris@119: end Chris@119: Chris@119: # Returns the identifier for the given Mercurial changeset Chris@119: def self.changeset_identifier(changeset) Chris@119: changeset.scmid Chris@119: end Chris@119: Chris@119: def diff_format_revisions(cs, cs_to, sep=':') Chris@119: super(cs, cs_to, ' ') Chris@119: end Chris@119: Chris@119: # Finds and returns a revision with a number or the beginning of a hash Chris@119: def find_changeset_by_name(name) Chris@1115: return nil if name.blank? Chris@1115: s = name.to_s Chris@1115: if /[^\d]/ =~ s or s.size > 8 Chris@1115: cs = changesets.where(:scmid => s).first Chris@119: else Chris@1115: cs = changesets.where(:revision => s).first Chris@119: end Chris@1115: return cs if cs Chris@1115: changesets.where('scmid LIKE ?', "#{s}%").first Chris@119: end Chris@119: Chris@119: # Returns the latest changesets for +path+; sorted by revision number Chris@441: # Chris@441: # Because :order => 'id DESC' is defined at 'has_many', Chris@441: # there is no need to set 'order'. Chris@441: # But, MySQL test fails. Chris@441: # Sqlite3 and PostgreSQL pass. Chris@441: # Is this MySQL bug? Chris@119: def latest_changesets(path, rev, limit=10) Chris@909: changesets.find(:all, Chris@909: :include => :user, Chris@441: :conditions => latest_changesets_cond(path, rev, limit), Chris@909: :limit => limit, Chris@909: :order => "#{Changeset.table_name}.id DESC") Chris@441: end Chris@441: Chris@441: def latest_changesets_cond(path, rev, limit) Chris@441: cond, args = [], [] Chris@441: if scm.branchmap.member? rev Chris@441: # Mercurial named branch is *stable* in each revision. Chris@441: # So, named branch can be stored in database. Chris@441: # Mercurial provides *bookmark* which is equivalent with git branch. Chris@441: # But, bookmark is not implemented. Chris@441: cond << "#{Changeset.table_name}.scmid IN (?)" Chris@441: # Revisions in root directory and sub directory are not equal. Chris@441: # So, in order to get correct limit, we need to get all revisions. Chris@441: # But, it is very heavy. Chris@441: # Mercurial does not treat direcotry. Chris@441: # So, "hg log DIR" is very heavy. Chris@441: branch_limit = path.blank? ? limit : ( limit * 5 ) Chris@441: args << scm.nodes_in_branch(rev, :limit => branch_limit) Chris@441: elsif last = rev ? find_changeset_by_name(scm.tagmap[rev] || rev) : nil Chris@441: cond << "#{Changeset.table_name}.id <= ?" Chris@441: args << last.id Chris@119: end Chris@441: unless path.blank? Chris@441: cond << "EXISTS (SELECT * FROM #{Change.table_name} Chris@441: WHERE #{Change.table_name}.changeset_id = #{Changeset.table_name}.id Chris@441: AND (#{Change.table_name}.path = ? Chris@441: OR #{Change.table_name}.path LIKE ? ESCAPE ?))" Chris@441: args << path.with_leading_slash Chris@909: args << "#{path.with_leading_slash.gsub(%r{[%_\\]}) { |s| "\\#{s}" }}/%" << '\\' Chris@441: end Chris@441: [cond.join(' AND '), *args] unless cond.empty? Chris@119: end Chris@441: private :latest_changesets_cond Chris@119: Chris@0: def fetch_changesets Chris@507: return if scm.info.nil? Chris@245: scm_rev = scm.info.lastrev.revision.to_i Chris@909: db_rev = latest_changeset ? latest_changeset.revision.to_i : -1 Chris@245: return unless db_rev < scm_rev # already up-to-date Chris@245: Chris@245: logger.debug "Fetching changesets for repository #{url}" if logger Chris@245: (db_rev + 1).step(scm_rev, FETCH_AT_ONCE) do |i| Chris@1115: scm.each_revision('', i, [i + FETCH_AT_ONCE - 1, scm_rev].min) do |re| Chris@1115: transaction do Chris@1115: parents = (re.parents || []).collect{|rp| find_changeset_by_name(rp)}.compact Chris@909: cs = Changeset.create(:repository => self, Chris@909: :revision => re.revision, Chris@909: :scmid => re.scmid, Chris@909: :committer => re.author, Chris@245: :committed_on => re.time, Chris@1115: :comments => re.message, Chris@1115: :parents => parents) Chris@1115: unless cs.new_record? Chris@1115: re.paths.each { |e| cs.create_change(e) } Chris@909: end Chris@0: end Chris@0: end Chris@0: end Chris@0: end Chris@0: end