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