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