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