annotate .svn/pristine/e8/e861f97486219bcc44af913a2690748a1ef6756a.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

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