Chris@0: # redMine - project management software Chris@0: # Copyright (C) 2006-2007 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@0: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@0: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@0: require 'redmine/scm/adapters/abstract_adapter' Chris@0: require 'rexml/document' Chris@0: Chris@0: module Redmine Chris@0: module Scm Chris@0: module Adapters Chris@0: class DarcsAdapter < AbstractAdapter Chris@0: # Darcs executable name Chris@0: DARCS_BIN = "darcs" Chris@0: Chris@0: class << self Chris@0: def client_version Chris@0: @@client_version ||= (darcs_binary_version || []) Chris@0: end Chris@0: Chris@0: def darcs_binary_version Chris@0: cmd = "#{DARCS_BIN} --version" Chris@0: version = nil Chris@0: shellout(cmd) do |io| Chris@0: # Read darcs version in first returned line Chris@0: if m = io.gets.match(%r{((\d+\.)+\d+)}) Chris@0: version = m[0].scan(%r{\d+}).collect(&:to_i) Chris@0: end Chris@0: end Chris@0: return nil if $? && $?.exitstatus != 0 Chris@0: version Chris@0: end Chris@0: end Chris@0: Chris@0: def initialize(url, root_url=nil, login=nil, password=nil) Chris@0: @url = url Chris@0: @root_url = url Chris@0: end Chris@0: Chris@0: def supports_cat? Chris@0: # cat supported in darcs 2.0.0 and higher Chris@0: self.class.client_version_above?([2, 0, 0]) Chris@0: end Chris@0: Chris@0: # Get info about the darcs repository Chris@0: def info Chris@0: rev = revisions(nil,nil,nil,{:limit => 1}) Chris@0: rev ? Info.new({:root_url => @url, :lastrev => rev.last}) : nil Chris@0: end Chris@0: Chris@0: # Returns an Entries collection Chris@0: # or nil if the given path doesn't exist in the repository Chris@0: def entries(path=nil, identifier=nil) Chris@0: path_prefix = (path.blank? ? '' : "#{path}/") Chris@0: path = '.' if path.blank? Chris@0: entries = Entries.new Chris@117: cmd = "#{DARCS_BIN} annotate --repodir #{shell_quote @url} --xml-output" Chris@0: cmd << " --match #{shell_quote("hash #{identifier}")}" if identifier Chris@0: cmd << " #{shell_quote path}" Chris@0: shellout(cmd) do |io| Chris@0: begin Chris@0: doc = REXML::Document.new(io) Chris@0: if doc.root.name == 'directory' Chris@0: doc.elements.each('directory/*') do |element| Chris@0: next unless ['file', 'directory'].include? element.name Chris@0: entries << entry_from_xml(element, path_prefix) Chris@0: end Chris@0: elsif doc.root.name == 'file' Chris@0: entries << entry_from_xml(doc.root, path_prefix) Chris@0: end Chris@0: rescue Chris@0: end Chris@0: end Chris@0: return nil if $? && $?.exitstatus != 0 Chris@0: entries.compact.sort_by_name Chris@0: end Chris@0: Chris@0: def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={}) Chris@0: path = '.' if path.blank? Chris@0: revisions = Revisions.new Chris@117: cmd = "#{DARCS_BIN} changes --repodir #{shell_quote @url} --xml-output" Chris@0: cmd << " --from-match #{shell_quote("hash #{identifier_from}")}" if identifier_from Chris@0: cmd << " --last #{options[:limit].to_i}" if options[:limit] Chris@0: shellout(cmd) do |io| Chris@0: begin Chris@0: doc = REXML::Document.new(io) Chris@0: doc.elements.each("changelog/patch") do |patch| Chris@0: message = patch.elements['name'].text Chris@0: message << "\n" + patch.elements['comment'].text.gsub(/\*\*\*END OF DESCRIPTION\*\*\*.*\z/m, '') if patch.elements['comment'] Chris@0: revisions << Revision.new({:identifier => nil, Chris@0: :author => patch.attributes['author'], Chris@0: :scmid => patch.attributes['hash'], Chris@0: :time => Time.parse(patch.attributes['local_date']), Chris@0: :message => message, Chris@0: :paths => (options[:with_path] ? get_paths_for_patch(patch.attributes['hash']) : nil) Chris@0: }) Chris@0: end Chris@0: rescue Chris@0: end Chris@0: end Chris@0: return nil if $? && $?.exitstatus != 0 Chris@0: revisions Chris@0: end Chris@0: Chris@0: def diff(path, identifier_from, identifier_to=nil) Chris@0: path = '*' if path.blank? Chris@117: cmd = "#{DARCS_BIN} diff --repodir #{shell_quote @url}" Chris@0: if identifier_to.nil? Chris@0: cmd << " --match #{shell_quote("hash #{identifier_from}")}" Chris@0: else Chris@0: cmd << " --to-match #{shell_quote("hash #{identifier_from}")}" Chris@0: cmd << " --from-match #{shell_quote("hash #{identifier_to}")}" Chris@0: end Chris@0: cmd << " -u #{shell_quote path}" Chris@0: diff = [] Chris@0: shellout(cmd) do |io| Chris@0: io.each_line do |line| Chris@0: diff << line Chris@0: end Chris@0: end Chris@0: return nil if $? && $?.exitstatus != 0 Chris@0: diff Chris@0: end Chris@0: Chris@0: def cat(path, identifier=nil) Chris@117: cmd = "#{DARCS_BIN} show content --repodir #{shell_quote @url}" Chris@0: cmd << " --match #{shell_quote("hash #{identifier}")}" if identifier Chris@0: cmd << " #{shell_quote path}" Chris@0: cat = nil Chris@0: shellout(cmd) do |io| Chris@0: io.binmode Chris@0: cat = io.read Chris@0: end Chris@0: return nil if $? && $?.exitstatus != 0 Chris@0: cat Chris@0: end Chris@0: Chris@0: private Chris@0: Chris@0: # Returns an Entry from the given XML element Chris@0: # or nil if the entry was deleted Chris@0: def entry_from_xml(element, path_prefix) Chris@0: modified_element = element.elements['modified'] Chris@0: if modified_element.elements['modified_how'].text.match(/removed/) Chris@0: return nil Chris@0: end Chris@0: Chris@0: Entry.new({:name => element.attributes['name'], Chris@0: :path => path_prefix + element.attributes['name'], Chris@0: :kind => element.name == 'file' ? 'file' : 'dir', Chris@0: :size => nil, Chris@0: :lastrev => Revision.new({ Chris@0: :identifier => nil, Chris@0: :scmid => modified_element.elements['patch'].attributes['hash'] Chris@0: }) Chris@0: }) Chris@0: end Chris@0: Chris@0: # Retrieve changed paths for a single patch Chris@0: def get_paths_for_patch(hash) Chris@117: cmd = "#{DARCS_BIN} annotate --repodir #{shell_quote @url} --summary --xml-output" Chris@0: cmd << " --match #{shell_quote("hash #{hash}")} " Chris@0: paths = [] Chris@0: shellout(cmd) do |io| Chris@0: begin Chris@0: # Darcs xml output has multiple root elements in this case (tested with darcs 1.0.7) Chris@0: # A root element is added so that REXML doesn't raise an error Chris@0: doc = REXML::Document.new("" + io.read + "") Chris@0: doc.elements.each('fake_root/summary/*') do |modif| Chris@0: paths << {:action => modif.name[0,1].upcase, Chris@0: :path => "/" + modif.text.chomp.gsub(/^\s*/, '') Chris@0: } Chris@0: end Chris@0: rescue Chris@0: end Chris@0: end Chris@0: paths Chris@0: rescue CommandFailed Chris@0: paths Chris@0: end Chris@0: end Chris@0: end Chris@0: end Chris@0: end