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