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