##// END OF EJS Templates
revert r13901...
Toshi MARUYAMA -
r13521:2cc3ba063fa0
parent child
Show More
@@ -1,220 +1,213
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2015 Jean-Philippe Lang
2 # Copyright (C) 2006-2015 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/cvs_adapter'
18 require 'redmine/scm/adapters/cvs_adapter'
19 require 'digest/sha1'
19 require 'digest/sha1'
20
20
21 class Repository::Cvs < Repository
21 class Repository::Cvs < Repository
22 validates_presence_of :url, :root_url, :log_encoding
22 validates_presence_of :url, :root_url, :log_encoding
23
23
24 safe_attributes 'root_url',
24 safe_attributes 'root_url',
25 :if => lambda {|repository, user| repository.new_record?}
25 :if => lambda {|repository, user| repository.new_record?}
26
26
27 def self.human_attribute_name(attribute_key_name, *args)
27 def self.human_attribute_name(attribute_key_name, *args)
28 attr_name = attribute_key_name.to_s
28 attr_name = attribute_key_name.to_s
29 if attr_name == "root_url"
29 if attr_name == "root_url"
30 attr_name = "cvsroot"
30 attr_name = "cvsroot"
31 elsif attr_name == "url"
31 elsif attr_name == "url"
32 attr_name = "cvs_module"
32 attr_name = "cvs_module"
33 end
33 end
34 super(attr_name, *args)
34 super(attr_name, *args)
35 end
35 end
36
36
37 def self.scm_adapter_class
37 def self.scm_adapter_class
38 Redmine::Scm::Adapters::CvsAdapter
38 Redmine::Scm::Adapters::CvsAdapter
39 end
39 end
40
40
41 def self.scm_name
41 def self.scm_name
42 'CVS'
42 'CVS'
43 end
43 end
44
44
45 def entry(path=nil, identifier=nil)
45 def entry(path=nil, identifier=nil)
46 rev = identifier.nil? ? nil : changesets.find_by_revision(identifier)
46 rev = identifier.nil? ? nil : changesets.find_by_revision(identifier)
47 scm.entry(path, rev.nil? ? nil : rev.committed_on)
47 scm.entry(path, rev.nil? ? nil : rev.committed_on)
48 end
48 end
49
49
50 def scm_entries(path=nil, identifier=nil)
50 def scm_entries(path=nil, identifier=nil)
51 rev = nil
51 rev = nil
52 if ! identifier.nil?
52 if ! identifier.nil?
53 rev = changesets.find_by_revision(identifier)
53 rev = changesets.find_by_revision(identifier)
54 return nil if rev.nil?
54 return nil if rev.nil?
55 end
55 end
56 entries = scm.entries(path, rev.nil? ? nil : rev.committed_on)
56 entries = scm.entries(path, rev.nil? ? nil : rev.committed_on)
57 if entries
57 if entries
58 entries.each() do |entry|
58 entries.each() do |entry|
59 if ( ! entry.lastrev.nil? ) && ( ! entry.lastrev.revision.nil? )
59 if ( ! entry.lastrev.nil? ) && ( ! entry.lastrev.revision.nil? )
60 if ActiveRecord::Base.connection.adapter_name =~ /sqlite/i &&
60 change = filechanges.find_by_revision_and_path(
61 Rails::VERSION::MAJOR == 4 && Rails::VERSION::MINOR == 2 &&
61 entry.lastrev.revision,
62 Rails::VERSION::TINY == 0
62 scm.with_leading_slash(entry.path) )
63 change_rev = filechanges.where(:revision => entry.lastrev.revision)
64 change = change_rev.find { |c| c.path == scm.with_leading_slash(entry.path) }
65 else
66 change = filechanges.where(
67 :revision => entry.lastrev.revision,
68 :path => scm.with_leading_slash(entry.path)).first
69 end
70 if change
63 if change
71 entry.lastrev.identifier = change.changeset.revision
64 entry.lastrev.identifier = change.changeset.revision
72 entry.lastrev.revision = change.changeset.revision
65 entry.lastrev.revision = change.changeset.revision
73 entry.lastrev.author = change.changeset.committer
66 entry.lastrev.author = change.changeset.committer
74 # entry.lastrev.branch = change.branch
67 # entry.lastrev.branch = change.branch
75 end
68 end
76 end
69 end
77 end
70 end
78 end
71 end
79 entries
72 entries
80 end
73 end
81 protected :scm_entries
74 protected :scm_entries
82
75
83 def cat(path, identifier=nil)
76 def cat(path, identifier=nil)
84 rev = nil
77 rev = nil
85 if ! identifier.nil?
78 if ! identifier.nil?
86 rev = changesets.find_by_revision(identifier)
79 rev = changesets.find_by_revision(identifier)
87 return nil if rev.nil?
80 return nil if rev.nil?
88 end
81 end
89 scm.cat(path, rev.nil? ? nil : rev.committed_on)
82 scm.cat(path, rev.nil? ? nil : rev.committed_on)
90 end
83 end
91
84
92 def annotate(path, identifier=nil)
85 def annotate(path, identifier=nil)
93 rev = nil
86 rev = nil
94 if ! identifier.nil?
87 if ! identifier.nil?
95 rev = changesets.find_by_revision(identifier)
88 rev = changesets.find_by_revision(identifier)
96 return nil if rev.nil?
89 return nil if rev.nil?
97 end
90 end
98 scm.annotate(path, rev.nil? ? nil : rev.committed_on)
91 scm.annotate(path, rev.nil? ? nil : rev.committed_on)
99 end
92 end
100
93
101 def diff(path, rev, rev_to)
94 def diff(path, rev, rev_to)
102 # convert rev to revision. CVS can't handle changesets here
95 # convert rev to revision. CVS can't handle changesets here
103 diff=[]
96 diff=[]
104 changeset_from = changesets.find_by_revision(rev)
97 changeset_from = changesets.find_by_revision(rev)
105 if rev_to.to_i > 0
98 if rev_to.to_i > 0
106 changeset_to = changesets.find_by_revision(rev_to)
99 changeset_to = changesets.find_by_revision(rev_to)
107 end
100 end
108 changeset_from.filechanges.each() do |change_from|
101 changeset_from.filechanges.each() do |change_from|
109 revision_from = nil
102 revision_from = nil
110 revision_to = nil
103 revision_to = nil
111 if path.nil? || (change_from.path.starts_with? scm.with_leading_slash(path))
104 if path.nil? || (change_from.path.starts_with? scm.with_leading_slash(path))
112 revision_from = change_from.revision
105 revision_from = change_from.revision
113 end
106 end
114 if revision_from
107 if revision_from
115 if changeset_to
108 if changeset_to
116 changeset_to.filechanges.each() do |change_to|
109 changeset_to.filechanges.each() do |change_to|
117 revision_to = change_to.revision if change_to.path == change_from.path
110 revision_to = change_to.revision if change_to.path == change_from.path
118 end
111 end
119 end
112 end
120 unless revision_to
113 unless revision_to
121 revision_to = scm.get_previous_revision(revision_from)
114 revision_to = scm.get_previous_revision(revision_from)
122 end
115 end
123 file_diff = scm.diff(change_from.path, revision_from, revision_to)
116 file_diff = scm.diff(change_from.path, revision_from, revision_to)
124 diff = diff + file_diff unless file_diff.nil?
117 diff = diff + file_diff unless file_diff.nil?
125 end
118 end
126 end
119 end
127 return diff
120 return diff
128 end
121 end
129
122
130 def fetch_changesets
123 def fetch_changesets
131 # some nifty bits to introduce a commit-id with cvs
124 # some nifty bits to introduce a commit-id with cvs
132 # natively cvs doesn't provide any kind of changesets,
125 # natively cvs doesn't provide any kind of changesets,
133 # there is only a revision per file.
126 # there is only a revision per file.
134 # we now take a guess using the author, the commitlog and the commit-date.
127 # we now take a guess using the author, the commitlog and the commit-date.
135
128
136 # last one is the next step to take. the commit-date is not equal for all
129 # last one is the next step to take. the commit-date is not equal for all
137 # commits in one changeset. cvs update the commit-date when the *,v file was touched. so
130 # commits in one changeset. cvs update the commit-date when the *,v file was touched. so
138 # we use a small delta here, to merge all changes belonging to _one_ changeset
131 # we use a small delta here, to merge all changes belonging to _one_ changeset
139 time_delta = 10.seconds
132 time_delta = 10.seconds
140 fetch_since = latest_changeset ? latest_changeset.committed_on : nil
133 fetch_since = latest_changeset ? latest_changeset.committed_on : nil
141 transaction do
134 transaction do
142 tmp_rev_num = 1
135 tmp_rev_num = 1
143 scm.revisions('', fetch_since, nil, :log_encoding => repo_log_encoding) do |revision|
136 scm.revisions('', fetch_since, nil, :log_encoding => repo_log_encoding) do |revision|
144 # only add the change to the database, if it doen't exists. the cvs log
137 # only add the change to the database, if it doen't exists. the cvs log
145 # is not exclusive at all.
138 # is not exclusive at all.
146 tmp_time = revision.time.clone
139 tmp_time = revision.time.clone
147 unless filechanges.find_by_path_and_revision(
140 unless filechanges.find_by_path_and_revision(
148 scm.with_leading_slash(revision.paths[0][:path]),
141 scm.with_leading_slash(revision.paths[0][:path]),
149 revision.paths[0][:revision]
142 revision.paths[0][:revision]
150 )
143 )
151 cmt = Changeset.normalize_comments(revision.message, repo_log_encoding)
144 cmt = Changeset.normalize_comments(revision.message, repo_log_encoding)
152 author_utf8 = Changeset.to_utf8(revision.author, repo_log_encoding)
145 author_utf8 = Changeset.to_utf8(revision.author, repo_log_encoding)
153 cs = changesets.where(
146 cs = changesets.where(
154 :committed_on => tmp_time - time_delta .. tmp_time + time_delta,
147 :committed_on => tmp_time - time_delta .. tmp_time + time_delta,
155 :committer => author_utf8,
148 :committer => author_utf8,
156 :comments => cmt
149 :comments => cmt
157 ).first
150 ).first
158 # create a new changeset....
151 # create a new changeset....
159 unless cs
152 unless cs
160 # we use a temporary revision number here (just for inserting)
153 # we use a temporary revision number here (just for inserting)
161 # later on, we calculate a continous positive number
154 # later on, we calculate a continous positive number
162 tmp_time2 = tmp_time.clone.gmtime
155 tmp_time2 = tmp_time.clone.gmtime
163 branch = revision.paths[0][:branch]
156 branch = revision.paths[0][:branch]
164 scmid = branch + "-" + tmp_time2.strftime("%Y%m%d-%H%M%S")
157 scmid = branch + "-" + tmp_time2.strftime("%Y%m%d-%H%M%S")
165 cs = Changeset.create(:repository => self,
158 cs = Changeset.create(:repository => self,
166 :revision => "tmp#{tmp_rev_num}",
159 :revision => "tmp#{tmp_rev_num}",
167 :scmid => scmid,
160 :scmid => scmid,
168 :committer => revision.author,
161 :committer => revision.author,
169 :committed_on => tmp_time,
162 :committed_on => tmp_time,
170 :comments => revision.message)
163 :comments => revision.message)
171 tmp_rev_num += 1
164 tmp_rev_num += 1
172 end
165 end
173 # convert CVS-File-States to internal Action-abbrevations
166 # convert CVS-File-States to internal Action-abbrevations
174 # default action is (M)odified
167 # default action is (M)odified
175 action = "M"
168 action = "M"
176 if revision.paths[0][:action] == "Exp" && revision.paths[0][:revision] == "1.1"
169 if revision.paths[0][:action] == "Exp" && revision.paths[0][:revision] == "1.1"
177 action = "A" # add-action always at first revision (= 1.1)
170 action = "A" # add-action always at first revision (= 1.1)
178 elsif revision.paths[0][:action] == "dead"
171 elsif revision.paths[0][:action] == "dead"
179 action = "D" # dead-state is similar to Delete
172 action = "D" # dead-state is similar to Delete
180 end
173 end
181 Change.create(
174 Change.create(
182 :changeset => cs,
175 :changeset => cs,
183 :action => action,
176 :action => action,
184 :path => scm.with_leading_slash(revision.paths[0][:path]),
177 :path => scm.with_leading_slash(revision.paths[0][:path]),
185 :revision => revision.paths[0][:revision],
178 :revision => revision.paths[0][:revision],
186 :branch => revision.paths[0][:branch]
179 :branch => revision.paths[0][:branch]
187 )
180 )
188 end
181 end
189 end
182 end
190
183
191 # Renumber new changesets in chronological order
184 # Renumber new changesets in chronological order
192 Changeset.
185 Changeset.
193 order('committed_on ASC, id ASC').
186 order('committed_on ASC, id ASC').
194 where("repository_id = ? AND revision LIKE 'tmp%'", id).
187 where("repository_id = ? AND revision LIKE 'tmp%'", id).
195 each do |changeset|
188 each do |changeset|
196 changeset.update_attribute :revision, next_revision_number
189 changeset.update_attribute :revision, next_revision_number
197 end
190 end
198 end # transaction
191 end # transaction
199 @current_revision_number = nil
192 @current_revision_number = nil
200 end
193 end
201
194
202 protected
195 protected
203
196
204 # Overrides Repository#validate_repository_path to validate
197 # Overrides Repository#validate_repository_path to validate
205 # against root_url attribute.
198 # against root_url attribute.
206 def validate_repository_path(attribute=:root_url)
199 def validate_repository_path(attribute=:root_url)
207 super(attribute)
200 super(attribute)
208 end
201 end
209
202
210 private
203 private
211
204
212 # Returns the next revision number to assign to a CVS changeset
205 # Returns the next revision number to assign to a CVS changeset
213 def next_revision_number
206 def next_revision_number
214 # Need to retrieve existing revision numbers to sort them as integers
207 # Need to retrieve existing revision numbers to sort them as integers
215 sql = "SELECT revision FROM #{Changeset.table_name} "
208 sql = "SELECT revision FROM #{Changeset.table_name} "
216 sql << "WHERE repository_id = #{id} AND revision NOT LIKE 'tmp%'"
209 sql << "WHERE repository_id = #{id} AND revision NOT LIKE 'tmp%'"
217 @current_revision_number ||= (self.class.connection.select_values(sql).collect(&:to_i).max || 0)
210 @current_revision_number ||= (self.class.connection.select_values(sql).collect(&:to_i).max || 0)
218 @current_revision_number += 1
211 @current_revision_number += 1
219 end
212 end
220 end
213 end
General Comments 0
You need to be logged in to leave comments. Login now