Chris@0
|
1 # redMine - project management software
|
Chris@0
|
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
|
Chris@0
|
3 #
|
Chris@0
|
4 # This program is free software; you can redistribute it and/or
|
Chris@0
|
5 # modify it under the terms of the GNU General Public License
|
Chris@0
|
6 # as published by the Free Software Foundation; either version 2
|
Chris@0
|
7 # of the License, or (at your option) any later version.
|
Chris@0
|
8 #
|
Chris@0
|
9 # This program is distributed in the hope that it will be useful,
|
Chris@0
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@0
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@0
|
12 # GNU General Public License for more details.
|
Chris@0
|
13 #
|
Chris@0
|
14 # You should have received a copy of the GNU General Public License
|
Chris@0
|
15 # along with this program; if not, write to the Free Software
|
Chris@0
|
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Chris@0
|
17
|
Chris@0
|
18 require 'redmine/scm/adapters/cvs_adapter'
|
Chris@0
|
19 require 'digest/sha1'
|
Chris@0
|
20
|
Chris@0
|
21 class Repository::Cvs < Repository
|
Chris@245
|
22 validates_presence_of :url, :root_url, :log_encoding
|
Chris@0
|
23
|
Chris@245
|
24 ATTRIBUTE_KEY_NAMES = {
|
Chris@245
|
25 "url" => "CVSROOT",
|
Chris@245
|
26 "root_url" => "Module",
|
Chris@245
|
27 "log_encoding" => "Commit messages encoding",
|
Chris@245
|
28 }
|
Chris@245
|
29 def self.human_attribute_name(attribute_key_name)
|
Chris@245
|
30 ATTRIBUTE_KEY_NAMES[attribute_key_name] || super
|
Chris@245
|
31 end
|
Chris@245
|
32
|
Chris@245
|
33 def self.scm_adapter_class
|
Chris@0
|
34 Redmine::Scm::Adapters::CvsAdapter
|
Chris@0
|
35 end
|
Chris@245
|
36
|
Chris@0
|
37 def self.scm_name
|
Chris@0
|
38 'CVS'
|
Chris@0
|
39 end
|
Chris@245
|
40
|
Chris@0
|
41 def entry(path=nil, identifier=nil)
|
Chris@0
|
42 rev = identifier.nil? ? nil : changesets.find_by_revision(identifier)
|
Chris@0
|
43 scm.entry(path, rev.nil? ? nil : rev.committed_on)
|
Chris@0
|
44 end
|
Chris@0
|
45
|
Chris@0
|
46 def entries(path=nil, identifier=nil)
|
Chris@0
|
47 rev = identifier.nil? ? nil : changesets.find_by_revision(identifier)
|
Chris@0
|
48 entries = scm.entries(path, rev.nil? ? nil : rev.committed_on)
|
Chris@0
|
49 if entries
|
Chris@0
|
50 entries.each() do |entry|
|
Chris@0
|
51 unless entry.lastrev.nil? || entry.lastrev.identifier
|
Chris@0
|
52 change=changes.find_by_revision_and_path( entry.lastrev.revision, scm.with_leading_slash(entry.path) )
|
Chris@0
|
53 if change
|
Chris@0
|
54 entry.lastrev.identifier=change.changeset.revision
|
Chris@0
|
55 entry.lastrev.author=change.changeset.committer
|
Chris@0
|
56 entry.lastrev.revision=change.revision
|
Chris@0
|
57 entry.lastrev.branch=change.branch
|
Chris@0
|
58 end
|
Chris@0
|
59 end
|
Chris@0
|
60 end
|
Chris@0
|
61 end
|
Chris@0
|
62 entries
|
Chris@0
|
63 end
|
Chris@0
|
64
|
Chris@0
|
65 def cat(path, identifier=nil)
|
Chris@0
|
66 rev = identifier.nil? ? nil : changesets.find_by_revision(identifier)
|
Chris@0
|
67 scm.cat(path, rev.nil? ? nil : rev.committed_on)
|
Chris@0
|
68 end
|
Chris@0
|
69
|
Chris@0
|
70 def diff(path, rev, rev_to)
|
Chris@0
|
71 #convert rev to revision. CVS can't handle changesets here
|
Chris@0
|
72 diff=[]
|
Chris@0
|
73 changeset_from=changesets.find_by_revision(rev)
|
Chris@0
|
74 if rev_to.to_i > 0
|
Chris@0
|
75 changeset_to=changesets.find_by_revision(rev_to)
|
Chris@0
|
76 end
|
Chris@0
|
77 changeset_from.changes.each() do |change_from|
|
Chris@0
|
78
|
Chris@0
|
79 revision_from=nil
|
Chris@0
|
80 revision_to=nil
|
Chris@0
|
81
|
Chris@0
|
82 revision_from=change_from.revision if path.nil? || (change_from.path.starts_with? scm.with_leading_slash(path))
|
Chris@0
|
83
|
Chris@0
|
84 if revision_from
|
Chris@0
|
85 if changeset_to
|
Chris@0
|
86 changeset_to.changes.each() do |change_to|
|
Chris@0
|
87 revision_to=change_to.revision if change_to.path==change_from.path
|
Chris@0
|
88 end
|
Chris@0
|
89 end
|
Chris@0
|
90 unless revision_to
|
Chris@0
|
91 revision_to=scm.get_previous_revision(revision_from)
|
Chris@0
|
92 end
|
Chris@0
|
93 file_diff = scm.diff(change_from.path, revision_from, revision_to)
|
Chris@0
|
94 diff = diff + file_diff unless file_diff.nil?
|
Chris@0
|
95 end
|
Chris@0
|
96 end
|
Chris@0
|
97 return diff
|
Chris@0
|
98 end
|
Chris@0
|
99
|
Chris@0
|
100 def fetch_changesets
|
Chris@0
|
101 # some nifty bits to introduce a commit-id with cvs
|
Chris@0
|
102 # natively cvs doesn't provide any kind of changesets, there is only a revision per file.
|
Chris@0
|
103 # we now take a guess using the author, the commitlog and the commit-date.
|
Chris@0
|
104
|
Chris@0
|
105 # last one is the next step to take. the commit-date is not equal for all
|
Chris@0
|
106 # commits in one changeset. cvs update the commit-date when the *,v file was touched. so
|
Chris@0
|
107 # we use a small delta here, to merge all changes belonging to _one_ changeset
|
Chris@0
|
108 time_delta=10.seconds
|
Chris@0
|
109
|
Chris@0
|
110 fetch_since = latest_changeset ? latest_changeset.committed_on : nil
|
Chris@0
|
111 transaction do
|
Chris@0
|
112 tmp_rev_num = 1
|
Chris@0
|
113 scm.revisions('', fetch_since, nil, :with_paths => true) do |revision|
|
Chris@0
|
114 # only add the change to the database, if it doen't exists. the cvs log
|
Chris@0
|
115 # is not exclusive at all.
|
Chris@210
|
116 tmp_time = revision.time.clone
|
Chris@210
|
117 unless changes.find_by_path_and_revision(
|
Chris@210
|
118 scm.with_leading_slash(revision.paths[0][:path]), revision.paths[0][:revision])
|
Chris@245
|
119 cmt = Changeset.normalize_comments(revision.message, repo_log_encoding)
|
Chris@0
|
120 cs = changesets.find(:first, :conditions=>{
|
Chris@210
|
121 :committed_on=>tmp_time - time_delta .. tmp_time + time_delta,
|
Chris@0
|
122 :committer=>revision.author,
|
Chris@245
|
123 :comments=>cmt
|
Chris@0
|
124 })
|
Chris@0
|
125
|
Chris@0
|
126 # create a new changeset....
|
Chris@0
|
127 unless cs
|
Chris@0
|
128 # we use a temporaray revision number here (just for inserting)
|
Chris@0
|
129 # later on, we calculate a continous positive number
|
Chris@210
|
130 tmp_time2 = tmp_time.clone.gmtime
|
Chris@210
|
131 branch = revision.paths[0][:branch]
|
Chris@210
|
132 scmid = branch + "-" + tmp_time2.strftime("%Y%m%d-%H%M%S")
|
Chris@0
|
133 cs = Changeset.create(:repository => self,
|
Chris@210
|
134 :revision => "tmp#{tmp_rev_num}",
|
Chris@210
|
135 :scmid => scmid,
|
Chris@0
|
136 :committer => revision.author,
|
Chris@210
|
137 :committed_on => tmp_time,
|
Chris@0
|
138 :comments => revision.message)
|
Chris@0
|
139 tmp_rev_num += 1
|
Chris@0
|
140 end
|
Chris@0
|
141
|
Chris@0
|
142 #convert CVS-File-States to internal Action-abbrevations
|
Chris@0
|
143 #default action is (M)odified
|
Chris@0
|
144 action="M"
|
Chris@0
|
145 if revision.paths[0][:action]=="Exp" && revision.paths[0][:revision]=="1.1"
|
Chris@0
|
146 action="A" #add-action always at first revision (= 1.1)
|
Chris@0
|
147 elsif revision.paths[0][:action]=="dead"
|
Chris@0
|
148 action="D" #dead-state is similar to Delete
|
Chris@0
|
149 end
|
Chris@0
|
150
|
Chris@0
|
151 Change.create(:changeset => cs,
|
Chris@0
|
152 :action => action,
|
Chris@0
|
153 :path => scm.with_leading_slash(revision.paths[0][:path]),
|
Chris@0
|
154 :revision => revision.paths[0][:revision],
|
Chris@0
|
155 :branch => revision.paths[0][:branch]
|
Chris@0
|
156 )
|
Chris@0
|
157 end
|
Chris@0
|
158 end
|
Chris@0
|
159
|
Chris@0
|
160 # Renumber new changesets in chronological order
|
Chris@210
|
161 changesets.find(
|
Chris@210
|
162 :all, :order => 'committed_on ASC, id ASC', :conditions => "revision LIKE 'tmp%'"
|
Chris@210
|
163 ).each do |changeset|
|
Chris@0
|
164 changeset.update_attribute :revision, next_revision_number
|
Chris@0
|
165 end
|
Chris@0
|
166 end # transaction
|
Chris@210
|
167 @current_revision_number = nil
|
Chris@0
|
168 end
|
Chris@0
|
169
|
Chris@0
|
170 private
|
Chris@0
|
171
|
Chris@0
|
172 # Returns the next revision number to assign to a CVS changeset
|
Chris@0
|
173 def next_revision_number
|
Chris@0
|
174 # Need to retrieve existing revision numbers to sort them as integers
|
Chris@210
|
175 sql = "SELECT revision FROM #{Changeset.table_name} "
|
Chris@210
|
176 sql << "WHERE repository_id = #{id} AND revision NOT LIKE 'tmp%'"
|
Chris@210
|
177 @current_revision_number ||= (connection.select_values(sql).collect(&:to_i).max || 0)
|
Chris@0
|
178 @current_revision_number += 1
|
Chris@0
|
179 end
|
Chris@0
|
180 end
|