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