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