OSDN Git Service

add Redmine trunk rev 3089
[redminele/redminele.git] / redmine / app / models / repository / darcs.rb
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/darcs_adapter'
19
20 class Repository::Darcs < Repository
21   validates_presence_of :url
22
23   def scm_adapter
24     Redmine::Scm::Adapters::DarcsAdapter
25   end
26   
27   def self.scm_name
28     'Darcs'
29   end
30   
31   def entry(path=nil, identifier=nil)
32     patch = identifier.nil? ? nil : changesets.find_by_revision(identifier)
33     scm.entry(path, patch.nil? ? nil : patch.scmid)
34   end
35   
36   def entries(path=nil, identifier=nil)
37     patch = identifier.nil? ? nil : changesets.find_by_revision(identifier)
38     entries = scm.entries(path, patch.nil? ? nil : patch.scmid)
39     if entries
40       entries.each do |entry|
41         # Search the DB for the entry's last change
42         changeset = changesets.find_by_scmid(entry.lastrev.scmid) if entry.lastrev && !entry.lastrev.scmid.blank?
43         if changeset
44           entry.lastrev.identifier = changeset.revision
45           entry.lastrev.name = changeset.revision
46           entry.lastrev.time = changeset.committed_on
47           entry.lastrev.author = changeset.committer
48         end
49       end
50     end
51     entries
52   end
53   
54   def cat(path, identifier=nil)
55     patch = identifier.nil? ? nil : changesets.find_by_revision(identifier.to_s)
56     scm.cat(path, patch.nil? ? nil : patch.scmid)
57   end
58   
59   def diff(path, rev, rev_to)
60     patch_from = changesets.find_by_revision(rev)
61     return nil if patch_from.nil?
62     patch_to = changesets.find_by_revision(rev_to) if rev_to
63     if path.blank?
64       path = patch_from.changes.collect{|change| change.path}.join(' ')
65     end
66     patch_from ? scm.diff(path, patch_from.scmid, patch_to ? patch_to.scmid : nil) : nil
67   end
68   
69   def fetch_changesets
70     scm_info = scm.info
71     if scm_info
72       db_last_id = latest_changeset ? latest_changeset.scmid : nil
73       next_rev = latest_changeset ? latest_changeset.revision.to_i + 1 : 1      
74       # latest revision in the repository
75       scm_revision = scm_info.lastrev.scmid      
76       unless changesets.find_by_scmid(scm_revision)
77         revisions = scm.revisions('', db_last_id, nil, :with_path => true)
78         transaction do
79           revisions.reverse_each do |revision|
80             changeset = Changeset.create(:repository => self,
81                                          :revision => next_rev,
82                                          :scmid => revision.scmid,
83                                          :committer => revision.author, 
84                                          :committed_on => revision.time,
85                                          :comments => revision.message)
86                                          
87             revision.paths.each do |change|
88               Change.create(:changeset => changeset,
89                             :action => change[:action],
90                             :path => change[:path],
91                             :from_path => change[:from_path],
92                             :from_revision => change[:from_revision])
93             end
94             next_rev += 1
95           end if revisions
96         end
97       end
98     end
99   end
100 end