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