annotate app/models/repository/cvs.rb @ 1298:4f746d8966dd redmine_2.3_integration

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