annotate .svn/pristine/e8/e861f97486219bcc44af913a2690748a1ef6756a.svn-base @ 1327:287f201c2802 redmine-2.2-integration

Add italic
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Wed, 19 Jun 2013 20:56:22 +0100
parents 038ba2d95de8
children
rev   line source
Chris@1296 1 # Redmine - project management software
Chris@1296 2 # Copyright (C) 2006-2012 Jean-Philippe Lang
Chris@1296 3 #
Chris@1296 4 # This program is free software; you can redistribute it and/or
Chris@1296 5 # modify it under the terms of the GNU General Public License
Chris@1296 6 # as published by the Free Software Foundation; either version 2
Chris@1296 7 # of the License, or (at your option) any later version.
Chris@1296 8 #
Chris@1296 9 # This program is distributed in the hope that it will be useful,
Chris@1296 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1296 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1296 12 # GNU General Public License for more details.
Chris@1296 13 #
Chris@1296 14 # You should have received a copy of the GNU General Public License
Chris@1296 15 # along with this program; if not, write to the Free Software
Chris@1296 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1296 17
Chris@1296 18 require 'redmine/scm/adapters/mercurial_adapter'
Chris@1296 19
Chris@1296 20 class Repository::Mercurial < Repository
Chris@1296 21 # sort changesets by revision number
Chris@1296 22 has_many :changesets,
Chris@1296 23 :order => "#{Changeset.table_name}.id DESC",
Chris@1296 24 :foreign_key => 'repository_id'
Chris@1296 25
Chris@1296 26 attr_protected :root_url
Chris@1296 27 validates_presence_of :url
Chris@1296 28
Chris@1296 29 # number of changesets to fetch at once
Chris@1296 30 FETCH_AT_ONCE = 100
Chris@1296 31
Chris@1296 32 def self.human_attribute_name(attribute_key_name, *args)
Chris@1296 33 attr_name = attribute_key_name.to_s
Chris@1296 34 if attr_name == "url"
Chris@1296 35 attr_name = "path_to_repository"
Chris@1296 36 end
Chris@1296 37 super(attr_name, *args)
Chris@1296 38 end
Chris@1296 39
Chris@1296 40 def self.scm_adapter_class
Chris@1296 41 Redmine::Scm::Adapters::MercurialAdapter
Chris@1296 42 end
Chris@1296 43
Chris@1296 44 def self.scm_name
Chris@1296 45 'Mercurial'
Chris@1296 46 end
Chris@1296 47
Chris@1296 48 def supports_directory_revisions?
Chris@1296 49 true
Chris@1296 50 end
Chris@1296 51
Chris@1296 52 def supports_revision_graph?
Chris@1296 53 true
Chris@1296 54 end
Chris@1296 55
Chris@1296 56 def repo_log_encoding
Chris@1296 57 'UTF-8'
Chris@1296 58 end
Chris@1296 59
Chris@1296 60 # Returns the readable identifier for the given mercurial changeset
Chris@1296 61 def self.format_changeset_identifier(changeset)
Chris@1296 62 "#{changeset.revision}:#{changeset.scmid}"
Chris@1296 63 end
Chris@1296 64
Chris@1296 65 # Returns the identifier for the given Mercurial changeset
Chris@1296 66 def self.changeset_identifier(changeset)
Chris@1296 67 changeset.scmid
Chris@1296 68 end
Chris@1296 69
Chris@1296 70 def diff_format_revisions(cs, cs_to, sep=':')
Chris@1296 71 super(cs, cs_to, ' ')
Chris@1296 72 end
Chris@1296 73
Chris@1296 74 # Finds and returns a revision with a number or the beginning of a hash
Chris@1296 75 def find_changeset_by_name(name)
Chris@1296 76 return nil if name.blank?
Chris@1296 77 s = name.to_s
Chris@1296 78 if /[^\d]/ =~ s or s.size > 8
Chris@1296 79 cs = changesets.where(:scmid => s).first
Chris@1296 80 else
Chris@1296 81 cs = changesets.where(:revision => s).first
Chris@1296 82 end
Chris@1296 83 return cs if cs
Chris@1296 84 changesets.where('scmid LIKE ?', "#{s}%").first
Chris@1296 85 end
Chris@1296 86
Chris@1296 87 # Returns the latest changesets for +path+; sorted by revision number
Chris@1296 88 #
Chris@1296 89 # Because :order => 'id DESC' is defined at 'has_many',
Chris@1296 90 # there is no need to set 'order'.
Chris@1296 91 # But, MySQL test fails.
Chris@1296 92 # Sqlite3 and PostgreSQL pass.
Chris@1296 93 # Is this MySQL bug?
Chris@1296 94 def latest_changesets(path, rev, limit=10)
Chris@1296 95 changesets.find(:all,
Chris@1296 96 :include => :user,
Chris@1296 97 :conditions => latest_changesets_cond(path, rev, limit),
Chris@1296 98 :limit => limit,
Chris@1296 99 :order => "#{Changeset.table_name}.id DESC")
Chris@1296 100 end
Chris@1296 101
Chris@1296 102 def latest_changesets_cond(path, rev, limit)
Chris@1296 103 cond, args = [], []
Chris@1296 104 if scm.branchmap.member? rev
Chris@1296 105 # Mercurial named branch is *stable* in each revision.
Chris@1296 106 # So, named branch can be stored in database.
Chris@1296 107 # Mercurial provides *bookmark* which is equivalent with git branch.
Chris@1296 108 # But, bookmark is not implemented.
Chris@1296 109 cond << "#{Changeset.table_name}.scmid IN (?)"
Chris@1296 110 # Revisions in root directory and sub directory are not equal.
Chris@1296 111 # So, in order to get correct limit, we need to get all revisions.
Chris@1296 112 # But, it is very heavy.
Chris@1296 113 # Mercurial does not treat direcotry.
Chris@1296 114 # So, "hg log DIR" is very heavy.
Chris@1296 115 branch_limit = path.blank? ? limit : ( limit * 5 )
Chris@1296 116 args << scm.nodes_in_branch(rev, :limit => branch_limit)
Chris@1296 117 elsif last = rev ? find_changeset_by_name(scm.tagmap[rev] || rev) : nil
Chris@1296 118 cond << "#{Changeset.table_name}.id <= ?"
Chris@1296 119 args << last.id
Chris@1296 120 end
Chris@1296 121 unless path.blank?
Chris@1296 122 cond << "EXISTS (SELECT * FROM #{Change.table_name}
Chris@1296 123 WHERE #{Change.table_name}.changeset_id = #{Changeset.table_name}.id
Chris@1296 124 AND (#{Change.table_name}.path = ?
Chris@1296 125 OR #{Change.table_name}.path LIKE ? ESCAPE ?))"
Chris@1296 126 args << path.with_leading_slash
Chris@1296 127 args << "#{path.with_leading_slash.gsub(%r{[%_\\]}) { |s| "\\#{s}" }}/%" << '\\'
Chris@1296 128 end
Chris@1296 129 [cond.join(' AND '), *args] unless cond.empty?
Chris@1296 130 end
Chris@1296 131 private :latest_changesets_cond
Chris@1296 132
Chris@1296 133 def fetch_changesets
Chris@1296 134 return if scm.info.nil?
Chris@1296 135 scm_rev = scm.info.lastrev.revision.to_i
Chris@1296 136 db_rev = latest_changeset ? latest_changeset.revision.to_i : -1
Chris@1296 137 return unless db_rev < scm_rev # already up-to-date
Chris@1296 138
Chris@1296 139 logger.debug "Fetching changesets for repository #{url}" if logger
Chris@1296 140 (db_rev + 1).step(scm_rev, FETCH_AT_ONCE) do |i|
Chris@1296 141 scm.each_revision('', i, [i + FETCH_AT_ONCE - 1, scm_rev].min) do |re|
Chris@1296 142 transaction do
Chris@1296 143 parents = (re.parents || []).collect{|rp| find_changeset_by_name(rp)}.compact
Chris@1296 144 cs = Changeset.create(:repository => self,
Chris@1296 145 :revision => re.revision,
Chris@1296 146 :scmid => re.scmid,
Chris@1296 147 :committer => re.author,
Chris@1296 148 :committed_on => re.time,
Chris@1296 149 :comments => re.message,
Chris@1296 150 :parents => parents)
Chris@1296 151 unless cs.new_record?
Chris@1296 152 re.paths.each { |e| cs.create_change(e) }
Chris@1296 153 end
Chris@1296 154 end
Chris@1296 155 end
Chris@1296 156 end
Chris@1296 157 end
Chris@1296 158 end