1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
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.
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.
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.
18 require 'redmine/scm/adapters/abstract_adapter'
19 require 'rexml/document'
24 class DarcsAdapter < AbstractAdapter
25 # Darcs executable name
28 def initialize(url, root_url=nil, login=nil, password=nil)
37 # Get info about the svn repository
39 rev = revisions(nil,nil,nil,{:limit => 1})
40 rev ? Info.new({:root_url => @url, :lastrev => rev.last}) : nil
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)
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?
56 cmd = "#{DARCS_BIN} annotate --repodir #{@url} --xml-output #{path}"
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)
65 elsif doc.root.name == 'file'
66 entries << entry_from_xml(doc.root, path_prefix)
71 return nil if $? && $?.exitstatus != 0
73 rescue Errno::ENOENT => e
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]
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']),
94 :paths => (options[:with_path] ? get_paths_for_patch(patch.attributes['hash']) : nil)
100 return nil if $? && $?.exitstatus != 0
102 rescue Errno::ENOENT => e
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
113 shellout(cmd) do |io|
114 io.each_line do |line|
118 return nil if $? && $?.exitstatus != 0
119 DiffTableList.new diff, type
120 rescue Errno::ENOENT => e
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',
131 :lastrev => Revision.new({
133 :scmid => element.elements['modified'].elements['patch'].attributes['hash']
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}\" "
143 shellout(cmd) do |io|
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*/, '')
157 rescue Errno::ENOENT => e