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, *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}"
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.blank?
77 s = name.to_s
78 if /[^\d]/ =~ s or s.size > 8
79 e = changesets.find(:first, :conditions => ['scmid = ?', s])
80 else
81 e = changesets.find(:first, :conditions => ['revision = ?', s])
82 end
83 return e if e
84 changesets.find(:first, :conditions => ['scmid LIKE ?', "#{s}%"]) # last ditch
85 end
86
87 # Returns the latest changesets for +path+; sorted by revision number
88 #
89 # Because :order => 'id DESC' is defined at 'has_many',
90 # there is no need to set 'order'.
91 # But, MySQL test fails.
92 # Sqlite3 and PostgreSQL pass.
93 # Is this MySQL bug?
94 def latest_changesets(path, rev, limit=10)
95 changesets.find(:all,
96 :include => :user,
97 :conditions => latest_changesets_cond(path, rev, limit),
98 :limit => limit,
99 :order => "#{Changeset.table_name}.id DESC")
100 end
101
102 def latest_changesets_cond(path, rev, limit)
103 cond, args = [], []
104 if scm.branchmap.member? rev
105 # Mercurial named branch is *stable* in each revision.
106 # So, named branch can be stored in database.
107 # Mercurial provides *bookmark* which is equivalent with git branch.
108 # But, bookmark is not implemented.
109 cond << "#{Changeset.table_name}.scmid IN (?)"
110 # Revisions in root directory and sub directory are not equal.
111 # So, in order to get correct limit, we need to get all revisions.
112 # But, it is very heavy.
113 # Mercurial does not treat direcotry.
114 # So, "hg log DIR" is very heavy.
115 branch_limit = path.blank? ? limit : ( limit * 5 )
116 args << scm.nodes_in_branch(rev, :limit => branch_limit)
117 elsif last = rev ? find_changeset_by_name(scm.tagmap[rev] || rev) : nil
118 cond << "#{Changeset.table_name}.id <= ?"
119 args << last.id
120 end
121 unless path.blank?
122 cond << "EXISTS (SELECT * FROM #{Change.table_name}
123 WHERE #{Change.table_name}.changeset_id = #{Changeset.table_name}.id
124 AND (#{Change.table_name}.path = ?
125 OR #{Change.table_name}.path LIKE ? ESCAPE ?))"
126 args << path.with_leading_slash
127 args << "#{path.with_leading_slash.gsub(%r{[%_\\]}) { |s| "\\#{s}" }}/%" << '\\'
128 end
129 [cond.join(' AND '), *args] unless cond.empty?
130 end
131 private :latest_changesets_cond
132
133 def fetch_changesets
134 return if scm.info.nil?
135 scm_rev = scm.info.lastrev.revision.to_i
136 db_rev = latest_changeset ? latest_changeset.revision.to_i : -1
137 return unless db_rev < scm_rev # already up-to-date
138
139 logger.debug "Fetching changesets for repository #{url}" if logger
140 (db_rev + 1).step(scm_rev, FETCH_AT_ONCE) do |i|
141 scm.each_revision('', i, [i + FETCH_AT_ONCE - 1, scm_rev].min) do |re|
142 transaction do
143 parents = (re.parents || []).collect{|rp| find_changeset_by_name(rp)}.compact
144 cs = Changeset.create(:repository => self,
145 :revision => re.revision,
146 :scmid => re.scmid,
147 :committer => re.author,
148 :committed_on => re.time,
149 :comments => re.message,
150 :parents => parents)
151 unless cs.new_record?
152 re.paths.each { |e| cs.create_change(e) }
153 end
154 end
155 end
156 end
157 end
158 end