Project

General

Profile

Patch #228 » visual_source_safe.rb

Aruo Miura, 2008-02-03 11:01

 
1
# redMine - project management software
2
# Copyright (C) 2006-2007  Jean-Philippe Lang
3
#
4
# This program is free software; you can redistribute it and/or
5
# modify it under the terms of the GNU General Public License
6
# as published by the Free Software Foundation; either version 2
7
# of the License, or (at your option) any later version.
8
# 
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
# 
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17

    
18
require 'redmine/scm/adapters/visual_source_safe_adapter'
19
require 'redmine/scm/adapters/abstract_adapter'
20

    
21
class Repository::VisualSourceSafe < Repository
22
  attr_protected :root_url
23
  validates_presence_of :url
24
  validates_format_of :url, :with => /^.*srcsafe\.ini/i
25

    
26
  def scm_adapter
27
    Redmine::Scm::Adapters::VisualSourceSafeAdapter
28
  end
29
  
30
  def self.scm_name
31
    'VisualSourceSafe'
32
  end
33

    
34
  def fetch_changesets
35
	#use like CVS
36

    
37
    #not the preferred way with CVS. maybe we should introduce always a cron-job for this
38
    last_commit = changesets.maximum(:committed_on)
39
    
40
    # some nifty bits to introduce a commit-id with cvs
41
    # natively cvs doesn't provide any kind of changesets, there is only a revision per file.
42
    # we now take a guess using the author, the commitlog and the commit-date.
43
    
44
    # last one is the next step to take. the commit-date is not equal for all 
45
    # commits in one changeset. cvs update the commit-date when the *,v file was touched. so
46
    # we use a small delta here, to merge all changes belonging to _one_ changeset
47
    #time_delta=10.seconds
48
    
49
    time_delta=10.seconds
50
    revisions = nil
51
    transaction do
52
      revisions = scm.revisions('', last_commit, nil, :with_paths => true)
53
      revisions.each do |revision|
54
        # only add the change to the database, if it doen't exists. the cvs log
55
        # is not exclusive at all. 
56
        cs1 = changes.find_by_path_and_revision(scm.with_leading_slash(revision.paths[0][:path]), revision.paths[0][:revision])
57
        unless cs1
58
          revision
59
          cs=Changeset.find(:first, :conditions=>{
60
            :committed_on=>revision.time-time_delta..revision.time+time_delta,
61
            :committer=>revision.author,
62
            :comments=>revision.message
63
          })
64
        
65
          # create a new changeset.... 
66
          unless cs 
67
            # we use a negative changeset-number here (just for inserting)
68
            # later on, we calculate a continous positive number
69
            next_rev = changesets.minimum(:revision)            
70
            next_rev = 0 if next_rev.nil? or next_rev > 0 
71
            next_rev = next_rev - 1
72
            
73
            cs=Changeset.create(:repository => self,
74
            :revision => next_rev, 
75
            :committer => revision.author, 
76
            :committed_on => revision.time,
77
            :comments => revision.message)
78
          end
79
        
80
          #convert CVS-File-States to internal Action-abbrevations
81
          #default action is (M)odified
82
          action="M"
83
#          if revision.paths[0][:action]=="Exp" && revision.paths[0][:revision]=="1.1"
84
#            action="A" #add-action always at first revision (= 1.1)
85
#          elsif revision.paths[0][:action]=="dead"
86
#            action="D" #dead-state is similar to Delete
87
#          end
88
        
89
          Change.create(:changeset => cs,
90
          :action => action,
91
          :path => scm.with_leading_slash(revision.paths[0][:path]),
92
          :revision => revision.paths[0][:revision],
93
          :branch => revision.paths[0][:branch]
94
          )
95
        end
96
      end
97
      
98
      next_rev = [changesets.maximum(:revision) || 0, 0].max
99
      changesets.find(:all, :conditions=>["revision < 0"], :order=>"committed_on ASC").each() do |changeset|
100
        next_rev = next_rev + 1
101
        changeset.revision = next_rev
102
        changeset.save!
103
      end
104
    end
105
  end
106

    
107
  def diff(path, rev, rev_to, type2)
108
    #convert rev to revision. CVS can't handle changesets here
109
    diffx = Redmine::Scm::Adapters::DiffTableList.new([], type2)
110
    changeset_from=changesets.find_by_revision(rev)
111
    if rev_to.to_i > 0 
112
      changeset_to=changesets.find_by_revision(rev_to)
113
    end
114
    changeset_from.changes.each() do |change_from|
115
      
116
      revision_from=nil
117
      revision_to=nil      
118
      
119
      revision_from=change_from.revision if path.nil? || (change_from.path.starts_with? scm.with_leading_slash(path))
120
      
121
      if revision_from
122
        if changeset_to
123
          changeset_to.changes.each() do |change_to|
124
            revision_to=change_to.revision if change_to.path==change_from.path 
125
          end
126
        end
127
        unless revision_to
128
          revision_to=scm.get_previous_revision(revision_from)
129
        end
130
        diff2 = scm.diff(change_from.path, revision_from, revision_to, type2)
131
        diffx=diff2 if diff2
132
      end
133
    end
134
    return diffx
135
  end
136

    
137
end
(5-5/8)