Chris@1494: # Redmine - project management software Chris@1494: # Copyright (C) 2006-2014 Jean-Philippe Lang Chris@1494: # Chris@1494: # This program is free software; you can redistribute it and/or Chris@1494: # modify it under the terms of the GNU General Public License Chris@1494: # as published by the Free Software Foundation; either version 2 Chris@1494: # of the License, or (at your option) any later version. Chris@1494: # Chris@1494: # This program is distributed in the hope that it will be useful, Chris@1494: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1494: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1494: # GNU General Public License for more details. Chris@1494: # Chris@1494: # You should have received a copy of the GNU General Public License Chris@1494: # along with this program; if not, write to the Free Software Chris@1494: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1494: Chris@1494: require 'redmine/scm/adapters/mercurial_adapter' Chris@1494: Chris@1494: class Repository::Mercurial < Repository Chris@1494: # sort changesets by revision number Chris@1494: has_many :changesets, Chris@1494: :order => "#{Changeset.table_name}.id DESC", Chris@1494: :foreign_key => 'repository_id' Chris@1494: Chris@1494: attr_protected :root_url Chris@1494: validates_presence_of :url Chris@1494: Chris@1494: # number of changesets to fetch at once Chris@1494: FETCH_AT_ONCE = 100 Chris@1494: Chris@1494: def self.human_attribute_name(attribute_key_name, *args) Chris@1494: attr_name = attribute_key_name.to_s Chris@1494: if attr_name == "url" Chris@1494: attr_name = "path_to_repository" Chris@1494: end Chris@1494: super(attr_name, *args) Chris@1494: end Chris@1494: Chris@1494: def self.scm_adapter_class Chris@1494: Redmine::Scm::Adapters::MercurialAdapter Chris@1494: end Chris@1494: Chris@1494: def self.scm_name Chris@1494: 'Mercurial' Chris@1494: end Chris@1494: Chris@1494: def supports_directory_revisions? Chris@1494: true Chris@1494: end Chris@1494: Chris@1494: def supports_revision_graph? Chris@1494: true Chris@1494: end Chris@1494: Chris@1494: def repo_log_encoding Chris@1494: 'UTF-8' Chris@1494: end Chris@1494: Chris@1494: # Returns the readable identifier for the given mercurial changeset Chris@1494: def self.format_changeset_identifier(changeset) Chris@1494: "#{changeset.revision}:#{changeset.scmid}" Chris@1494: end Chris@1494: Chris@1494: # Returns the identifier for the given Mercurial changeset Chris@1494: def self.changeset_identifier(changeset) Chris@1494: changeset.scmid Chris@1494: end Chris@1494: Chris@1494: def diff_format_revisions(cs, cs_to, sep=':') Chris@1494: super(cs, cs_to, ' ') Chris@1494: end Chris@1494: Chris@1494: # Finds and returns a revision with a number or the beginning of a hash Chris@1494: def find_changeset_by_name(name) Chris@1494: return nil if name.blank? Chris@1494: s = name.to_s Chris@1494: if /[^\d]/ =~ s or s.size > 8 Chris@1494: cs = changesets.where(:scmid => s).first Chris@1494: else Chris@1494: cs = changesets.where(:revision => s).first Chris@1494: end Chris@1494: return cs if cs Chris@1494: changesets.where('scmid LIKE ?', "#{s}%").first Chris@1494: end Chris@1494: Chris@1494: # Returns the latest changesets for +path+; sorted by revision number Chris@1494: # Chris@1494: # Because :order => 'id DESC' is defined at 'has_many', Chris@1494: # there is no need to set 'order'. Chris@1494: # But, MySQL test fails. Chris@1494: # Sqlite3 and PostgreSQL pass. Chris@1494: # Is this MySQL bug? Chris@1494: def latest_changesets(path, rev, limit=10) Chris@1494: changesets. Chris@1494: includes(:user). Chris@1494: where(latest_changesets_cond(path, rev, limit)). Chris@1494: limit(limit). Chris@1494: order("#{Changeset.table_name}.id DESC"). Chris@1494: all Chris@1494: end Chris@1494: Chris@1494: def latest_changesets_cond(path, rev, limit) Chris@1494: cond, args = [], [] Chris@1494: if scm.branchmap.member? rev Chris@1494: # Mercurial named branch is *stable* in each revision. Chris@1494: # So, named branch can be stored in database. Chris@1494: # Mercurial provides *bookmark* which is equivalent with git branch. Chris@1494: # But, bookmark is not implemented. Chris@1494: cond << "#{Changeset.table_name}.scmid IN (?)" Chris@1494: # Revisions in root directory and sub directory are not equal. Chris@1494: # So, in order to get correct limit, we need to get all revisions. Chris@1494: # But, it is very heavy. Chris@1494: # Mercurial does not treat direcotry. Chris@1494: # So, "hg log DIR" is very heavy. Chris@1494: branch_limit = path.blank? ? limit : ( limit * 5 ) Chris@1494: args << scm.nodes_in_branch(rev, :limit => branch_limit) Chris@1494: elsif last = rev ? find_changeset_by_name(scm.tagmap[rev] || rev) : nil Chris@1494: cond << "#{Changeset.table_name}.id <= ?" Chris@1494: args << last.id Chris@1494: end Chris@1494: unless path.blank? Chris@1494: cond << "EXISTS (SELECT * FROM #{Change.table_name} Chris@1494: WHERE #{Change.table_name}.changeset_id = #{Changeset.table_name}.id Chris@1494: AND (#{Change.table_name}.path = ? Chris@1494: OR #{Change.table_name}.path LIKE ? ESCAPE ?))" Chris@1494: args << path.with_leading_slash Chris@1494: args << "#{path.with_leading_slash.gsub(%r{[%_\\]}) { |s| "\\#{s}" }}/%" << '\\' Chris@1494: end Chris@1494: [cond.join(' AND '), *args] unless cond.empty? Chris@1494: end Chris@1494: private :latest_changesets_cond Chris@1494: Chris@1494: def fetch_changesets Chris@1494: return if scm.info.nil? Chris@1494: scm_rev = scm.info.lastrev.revision.to_i Chris@1494: db_rev = latest_changeset ? latest_changeset.revision.to_i : -1 Chris@1494: return unless db_rev < scm_rev # already up-to-date Chris@1494: Chris@1494: logger.debug "Fetching changesets for repository #{url}" if logger Chris@1494: (db_rev + 1).step(scm_rev, FETCH_AT_ONCE) do |i| Chris@1494: scm.each_revision('', i, [i + FETCH_AT_ONCE - 1, scm_rev].min) do |re| Chris@1494: transaction do Chris@1494: parents = (re.parents || []).collect{|rp| find_changeset_by_name(rp)}.compact Chris@1494: cs = Changeset.create(:repository => self, Chris@1494: :revision => re.revision, Chris@1494: :scmid => re.scmid, Chris@1494: :committer => re.author, Chris@1494: :committed_on => re.time, Chris@1494: :comments => re.message, Chris@1494: :parents => parents) Chris@1494: unless cs.new_record? Chris@1494: re.paths.each { |e| cs.create_change(e) } Chris@1494: end Chris@1494: end Chris@1494: end Chris@1494: end Chris@1494: end Chris@1494: end