Chris@0: # redMine - project management software Chris@0: # Copyright (C) 2006-2007 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@0: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@0: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@0: require 'redmine/scm/adapters/cvs_adapter' Chris@0: require 'digest/sha1' Chris@0: Chris@0: class Repository::Cvs < Repository Chris@0: validates_presence_of :url, :root_url Chris@0: Chris@0: def scm_adapter Chris@0: Redmine::Scm::Adapters::CvsAdapter Chris@0: end Chris@0: Chris@0: def self.scm_name Chris@0: 'CVS' Chris@0: end Chris@0: Chris@0: def entry(path=nil, identifier=nil) Chris@0: rev = identifier.nil? ? nil : changesets.find_by_revision(identifier) Chris@0: scm.entry(path, rev.nil? ? nil : rev.committed_on) Chris@0: end Chris@0: Chris@0: def entries(path=nil, identifier=nil) Chris@0: rev = identifier.nil? ? nil : changesets.find_by_revision(identifier) Chris@0: entries = scm.entries(path, rev.nil? ? nil : rev.committed_on) Chris@0: if entries Chris@0: entries.each() do |entry| Chris@0: unless entry.lastrev.nil? || entry.lastrev.identifier Chris@0: change=changes.find_by_revision_and_path( entry.lastrev.revision, scm.with_leading_slash(entry.path) ) Chris@0: if change Chris@0: entry.lastrev.identifier=change.changeset.revision Chris@0: entry.lastrev.author=change.changeset.committer Chris@0: entry.lastrev.revision=change.revision Chris@0: entry.lastrev.branch=change.branch Chris@0: end Chris@0: end Chris@0: end Chris@0: end Chris@0: entries Chris@0: end Chris@0: Chris@0: def cat(path, identifier=nil) Chris@0: rev = identifier.nil? ? nil : changesets.find_by_revision(identifier) Chris@0: scm.cat(path, rev.nil? ? nil : rev.committed_on) Chris@0: end Chris@0: Chris@0: def diff(path, rev, rev_to) Chris@0: #convert rev to revision. CVS can't handle changesets here Chris@0: diff=[] Chris@0: changeset_from=changesets.find_by_revision(rev) Chris@0: if rev_to.to_i > 0 Chris@0: changeset_to=changesets.find_by_revision(rev_to) Chris@0: end Chris@0: changeset_from.changes.each() do |change_from| Chris@0: Chris@0: revision_from=nil Chris@0: revision_to=nil Chris@0: Chris@0: revision_from=change_from.revision if path.nil? || (change_from.path.starts_with? scm.with_leading_slash(path)) Chris@0: Chris@0: if revision_from Chris@0: if changeset_to Chris@0: changeset_to.changes.each() do |change_to| Chris@0: revision_to=change_to.revision if change_to.path==change_from.path Chris@0: end Chris@0: end Chris@0: unless revision_to Chris@0: revision_to=scm.get_previous_revision(revision_from) Chris@0: end Chris@0: file_diff = scm.diff(change_from.path, revision_from, revision_to) Chris@0: diff = diff + file_diff unless file_diff.nil? Chris@0: end Chris@0: end Chris@0: return diff Chris@0: end Chris@0: Chris@0: def fetch_changesets Chris@0: # some nifty bits to introduce a commit-id with cvs Chris@0: # natively cvs doesn't provide any kind of changesets, there is only a revision per file. Chris@0: # we now take a guess using the author, the commitlog and the commit-date. Chris@0: Chris@0: # last one is the next step to take. the commit-date is not equal for all Chris@0: # commits in one changeset. cvs update the commit-date when the *,v file was touched. so Chris@0: # we use a small delta here, to merge all changes belonging to _one_ changeset Chris@0: time_delta=10.seconds Chris@0: Chris@0: fetch_since = latest_changeset ? latest_changeset.committed_on : nil Chris@0: transaction do Chris@0: tmp_rev_num = 1 Chris@0: scm.revisions('', fetch_since, nil, :with_paths => true) do |revision| Chris@0: # only add the change to the database, if it doen't exists. the cvs log Chris@0: # is not exclusive at all. Chris@0: unless changes.find_by_path_and_revision(scm.with_leading_slash(revision.paths[0][:path]), revision.paths[0][:revision]) Chris@0: revision Chris@0: cs = changesets.find(:first, :conditions=>{ Chris@0: :committed_on=>revision.time-time_delta..revision.time+time_delta, Chris@0: :committer=>revision.author, Chris@0: :comments=>Changeset.normalize_comments(revision.message) Chris@0: }) Chris@0: Chris@0: # create a new changeset.... Chris@0: unless cs Chris@0: # we use a temporaray revision number here (just for inserting) Chris@0: # later on, we calculate a continous positive number Chris@0: latest = changesets.find(:first, :order => 'id DESC') Chris@0: cs = Changeset.create(:repository => self, Chris@0: :revision => "_#{tmp_rev_num}", Chris@0: :committer => revision.author, Chris@0: :committed_on => revision.time, Chris@0: :comments => revision.message) Chris@0: tmp_rev_num += 1 Chris@0: end Chris@0: Chris@0: #convert CVS-File-States to internal Action-abbrevations Chris@0: #default action is (M)odified Chris@0: action="M" Chris@0: if revision.paths[0][:action]=="Exp" && revision.paths[0][:revision]=="1.1" Chris@0: action="A" #add-action always at first revision (= 1.1) Chris@0: elsif revision.paths[0][:action]=="dead" Chris@0: action="D" #dead-state is similar to Delete Chris@0: end Chris@0: Chris@0: Change.create(:changeset => cs, Chris@0: :action => action, Chris@0: :path => scm.with_leading_slash(revision.paths[0][:path]), Chris@0: :revision => revision.paths[0][:revision], Chris@0: :branch => revision.paths[0][:branch] Chris@0: ) Chris@0: end Chris@0: end Chris@0: Chris@0: # Renumber new changesets in chronological order Chris@0: changesets.find(:all, :order => 'committed_on ASC, id ASC', :conditions => "revision LIKE '_%'").each do |changeset| Chris@0: changeset.update_attribute :revision, next_revision_number Chris@0: end Chris@0: end # transaction Chris@0: end Chris@0: Chris@0: private Chris@0: Chris@0: # Returns the next revision number to assign to a CVS changeset Chris@0: def next_revision_number Chris@0: # Need to retrieve existing revision numbers to sort them as integers Chris@0: @current_revision_number ||= (connection.select_values("SELECT revision FROM #{Changeset.table_name} WHERE repository_id = #{id} AND revision NOT LIKE '_%'").collect(&:to_i).max || 0) Chris@0: @current_revision_number += 1 Chris@0: end Chris@0: end