To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / app / models / repository / mercurial.rb @ 1533:59e13100ea95
History | View | Annotate | Download (6.71 KB)
| 1 |
# Redmine - project management software
|
|---|---|
| 2 |
# Copyright (C) 2006-2014 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_dependency '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, *args) |
| 33 |
attr_name = attribute_key_name.to_s |
| 34 |
if attr_name == "url" |
| 35 |
attr_name = "path_to_repository"
|
| 36 |
end
|
| 37 |
super(attr_name, *args)
|
| 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[0, 12]}"
|
| 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 |
def modify_entry_lastrev_identifier(entry) |
| 75 |
if entry.lastrev && entry.lastrev.identifier
|
| 76 |
entry.lastrev.identifier = scmid_for_inserting_db(entry.lastrev.identifier) |
| 77 |
end
|
| 78 |
end
|
| 79 |
private :modify_entry_lastrev_identifier
|
| 80 |
|
| 81 |
def entry(path=nil, identifier=nil) |
| 82 |
entry = scm.entry(path, identifier) |
| 83 |
return nil if entry.nil? |
| 84 |
modify_entry_lastrev_identifier(entry) |
| 85 |
entry |
| 86 |
end
|
| 87 |
|
| 88 |
def scm_entries(path=nil, identifier=nil) |
| 89 |
entries = scm.entries(path, identifier) |
| 90 |
return nil if entries.nil? |
| 91 |
entries.each {|entry| modify_entry_lastrev_identifier(entry)}
|
| 92 |
entries |
| 93 |
end
|
| 94 |
protected :scm_entries
|
| 95 |
|
| 96 |
# Finds and returns a revision with a number or the beginning of a hash
|
| 97 |
def find_changeset_by_name(name) |
| 98 |
return nil if name.blank? |
| 99 |
s = name.to_s |
| 100 |
if /[^\d]/ =~ s or s.size > 8 |
| 101 |
cs = changesets.where(:scmid => s).first
|
| 102 |
else
|
| 103 |
cs = changesets.where(:revision => s).first
|
| 104 |
end
|
| 105 |
return cs if cs |
| 106 |
changesets.where('scmid LIKE ?', "#{s}%").first |
| 107 |
end
|
| 108 |
|
| 109 |
# Returns the latest changesets for +path+; sorted by revision number
|
| 110 |
#
|
| 111 |
# Because :order => 'id DESC' is defined at 'has_many',
|
| 112 |
# there is no need to set 'order'.
|
| 113 |
# But, MySQL test fails.
|
| 114 |
# Sqlite3 and PostgreSQL pass.
|
| 115 |
# Is this MySQL bug?
|
| 116 |
def latest_changesets(path, rev, limit=10) |
| 117 |
changesets. |
| 118 |
includes(:user).
|
| 119 |
where(latest_changesets_cond(path, rev, limit)). |
| 120 |
limit(limit). |
| 121 |
order("#{Changeset.table_name}.id DESC").
|
| 122 |
all |
| 123 |
end
|
| 124 |
|
| 125 |
def is_short_id_in_db? |
| 126 |
return @is_short_id_in_db unless @is_short_id_in_db.nil? |
| 127 |
cs = changesets.first |
| 128 |
@is_short_id_in_db = (!cs.nil? && cs.scmid.length != 40) |
| 129 |
end
|
| 130 |
private :is_short_id_in_db?
|
| 131 |
|
| 132 |
def scmid_for_inserting_db(scmid) |
| 133 |
is_short_id_in_db? ? scmid[0, 12] : scmid |
| 134 |
end
|
| 135 |
|
| 136 |
def nodes_in_branch(rev, branch_limit) |
| 137 |
scm.nodes_in_branch(rev, :limit => branch_limit).collect do |b| |
| 138 |
scmid_for_inserting_db(b) |
| 139 |
end
|
| 140 |
end
|
| 141 |
|
| 142 |
def tag_scmid(rev) |
| 143 |
scmid = scm.tagmap[rev] |
| 144 |
scmid.nil? ? nil : scmid_for_inserting_db(scmid)
|
| 145 |
end
|
| 146 |
|
| 147 |
def latest_changesets_cond(path, rev, limit) |
| 148 |
cond, args = [], [] |
| 149 |
if scm.branchmap.member? rev
|
| 150 |
# Mercurial named branch is *stable* in each revision.
|
| 151 |
# So, named branch can be stored in database.
|
| 152 |
# Mercurial provides *bookmark* which is equivalent with git branch.
|
| 153 |
# But, bookmark is not implemented.
|
| 154 |
cond << "#{Changeset.table_name}.scmid IN (?)"
|
| 155 |
# Revisions in root directory and sub directory are not equal.
|
| 156 |
# So, in order to get correct limit, we need to get all revisions.
|
| 157 |
# But, it is very heavy.
|
| 158 |
# Mercurial does not treat direcotry.
|
| 159 |
# So, "hg log DIR" is very heavy.
|
| 160 |
branch_limit = path.blank? ? limit : ( limit * 5 )
|
| 161 |
args << nodes_in_branch(rev, branch_limit) |
| 162 |
elsif last = rev ? find_changeset_by_name(tag_scmid(rev) || rev) : nil |
| 163 |
cond << "#{Changeset.table_name}.id <= ?"
|
| 164 |
args << last.id |
| 165 |
end
|
| 166 |
unless path.blank?
|
| 167 |
cond << "EXISTS (SELECT * FROM #{Change.table_name}
|
| 168 |
WHERE #{Change.table_name}.changeset_id = #{Changeset.table_name}.id
|
| 169 |
AND (#{Change.table_name}.path = ?
|
| 170 |
OR #{Change.table_name}.path LIKE ? ESCAPE ?))"
|
| 171 |
args << path.with_leading_slash |
| 172 |
args << "#{path.with_leading_slash.gsub(%r{[%_\\]}) { |s| "\\#{s}" }}/%" << '\\' |
| 173 |
end
|
| 174 |
[cond.join(' AND '), *args] unless cond.empty? |
| 175 |
end
|
| 176 |
private :latest_changesets_cond
|
| 177 |
|
| 178 |
def fetch_changesets |
| 179 |
return if scm.info.nil? |
| 180 |
scm_rev = scm.info.lastrev.revision.to_i |
| 181 |
db_rev = latest_changeset ? latest_changeset.revision.to_i : -1
|
| 182 |
return unless db_rev < scm_rev # already up-to-date |
| 183 |
|
| 184 |
logger.debug "Fetching changesets for repository #{url}" if logger |
| 185 |
(db_rev + 1).step(scm_rev, FETCH_AT_ONCE) do |i| |
| 186 |
scm.each_revision('', i, [i + FETCH_AT_ONCE - 1, scm_rev].min) do |re| |
| 187 |
transaction do
|
| 188 |
parents = (re.parents || []).collect do |rp|
|
| 189 |
find_changeset_by_name(scmid_for_inserting_db(rp)) |
| 190 |
end.compact
|
| 191 |
cs = Changeset.create(:repository => self, |
| 192 |
:revision => re.revision,
|
| 193 |
:scmid => scmid_for_inserting_db(re.scmid),
|
| 194 |
:committer => re.author,
|
| 195 |
:committed_on => re.time,
|
| 196 |
:comments => re.message,
|
| 197 |
:parents => parents)
|
| 198 |
unless cs.new_record?
|
| 199 |
re.paths.each do |e|
|
| 200 |
if from_revision = e[:from_revision] |
| 201 |
e[:from_revision] = scmid_for_inserting_db(from_revision)
|
| 202 |
end
|
| 203 |
cs.create_change(e) |
| 204 |
end
|
| 205 |
end
|
| 206 |
end
|
| 207 |
end
|
| 208 |
end
|
| 209 |
end
|
| 210 |
end
|