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