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