OSDN Git Service

Added Darcs basic support.
[redminele/redmine.git] / lib / redmine / scm / adapters / darcs_adapter.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/abstract_adapter'
19 require 'rexml/document'
20
21 module Redmine
22   module Scm
23     module Adapters    
24       class DarcsAdapter < AbstractAdapter      
25         # Darcs executable name
26         DARCS_BIN = "darcs"
27         
28         def initialize(url, root_url=nil, login=nil, password=nil)
29           @url = url
30           @root_url = url
31         end
32
33         def supports_cat?
34           false
35         end
36               
37         # Get info about the svn repository
38         def info
39           rev = revisions(nil,nil,nil,{:limit => 1})
40           rev ? Info.new({:root_url => @url, :lastrev => rev.last}) : nil
41         end
42         
43         # Returns the entry identified by path and revision identifier
44         # or nil if entry doesn't exist in the repository
45         def entry(path=nil, identifier=nil)
46           e = entries(path, identifier)
47           e ? e.first : nil
48         end
49         
50         # Returns an Entries collection
51         # or nil if the given path doesn't exist in the repository
52         def entries(path=nil, identifier=nil)
53           path_prefix = (path.blank? ? '' : "#{path}/")
54           path = '.' if path.blank?
55           entries = Entries.new          
56           cmd = "#{DARCS_BIN} annotate --repodir #{@url} --xml-output #{path}"
57           shellout(cmd) do |io|
58             begin
59               doc = REXML::Document.new(io)
60               if doc.root.name == 'directory'
61                 doc.elements.each('directory/*') do |element|
62                   next unless ['file', 'directory'].include? element.name
63                   entries << entry_from_xml(element, path_prefix)
64                 end
65               elsif doc.root.name == 'file'
66                 entries << entry_from_xml(doc.root, path_prefix)
67               end
68             rescue
69             end
70           end
71           return nil if $? && $?.exitstatus != 0
72           entries.sort_by_name
73         rescue Errno::ENOENT => e
74           raise CommandFailed
75         end
76     
77         def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
78           path = '.' if path.blank?
79           revisions = Revisions.new
80           cmd = "#{DARCS_BIN} changes --repodir #{@url} --xml-output"
81           cmd << " --from-match \"hash #{identifier_from}\"" if identifier_from
82           cmd << " --last #{options[:limit].to_i}" if options[:limit]
83           shellout(cmd) do |io|
84             begin
85               doc = REXML::Document.new(io)
86               doc.elements.each("changelog/patch") do |patch|
87                 message = patch.elements['name'].text
88                 message << "\n" + patch.elements['comment'].text.gsub(/\*\*\*END OF DESCRIPTION\*\*\*.*\z/m, '') if patch.elements['comment']
89                 revisions << Revision.new({:identifier => nil,
90                               :author => patch.attributes['author'],
91                               :scmid => patch.attributes['hash'],
92                               :time => Time.parse(patch.attributes['local_date']),
93                               :message => message,
94                               :paths => (options[:with_path] ? get_paths_for_patch(patch.attributes['hash']) : nil)
95                             })
96               end
97             rescue
98             end
99           end
100           return nil if $? && $?.exitstatus != 0
101           revisions
102         rescue Errno::ENOENT => e
103           raise CommandFailed    
104         end
105         
106         def diff(path, identifier_from, identifier_to=nil, type="inline")
107           path = '*' if path.blank?
108           cmd = "#{DARCS_BIN} diff --repodir #{@url}"
109           cmd << " --to-match \"hash #{identifier_from}\""
110           cmd << " --from-match \"hash #{identifier_to}\"" if identifier_to
111           cmd << " -u #{path}"
112           diff = []
113           shellout(cmd) do |io|
114             io.each_line do |line|
115               diff << line
116             end
117           end
118           return nil if $? && $?.exitstatus != 0
119           DiffTableList.new diff, type    
120         rescue Errno::ENOENT => e
121           raise CommandFailed    
122         end
123         
124         private
125                 
126         def entry_from_xml(element, path_prefix)
127           Entry.new({:name => element.attributes['name'],
128                      :path => path_prefix + element.attributes['name'],
129                      :kind => element.name == 'file' ? 'file' : 'dir',
130                      :size => nil,
131                      :lastrev => Revision.new({
132                        :identifier => nil,
133                        :scmid => element.elements['modified'].elements['patch'].attributes['hash']
134                        })
135                      })        
136         end
137         
138         # Retrieve changed paths for a single patch
139         def get_paths_for_patch(hash)
140           cmd = "#{DARCS_BIN} annotate --repodir #{@url} --summary --xml-output"
141           cmd << " --match \"hash #{hash}\" "
142           paths = []
143           shellout(cmd) do |io|
144             begin
145               # Darcs xml output has multiple root elements in this case (tested with darcs 1.0.7)
146               # A root element is added so that REXML doesn't raise an error
147               doc = REXML::Document.new("<fake_root>" + io.read + "</fake_root>")
148               doc.elements.each('fake_root/summary/*') do |modif|
149                 paths << {:action => modif.name[0,1].upcase,
150                           :path => "/" + modif.text.chomp.gsub(/^\s*/, '')
151                          }
152               end
153             rescue
154             end
155           end
156           paths
157         rescue Errno::ENOENT => e
158           paths
159         end
160       end
161     end
162   end
163 end